Opened 4 years ago

Closed 4 years ago

#1060 closed ожидается проверка (задача сдана)

WW #18

Reported by: Alexander Morozov Owned by: Egor Suvorov
Component: WW_format Version: 1.0
Keywords: Cc:

Description


Change History (1)

comment:1 Changed 4 years ago by Egor Suvorov

Resolution: задача сдана
Status: assignedclosed

Корректность 2.5/7.
Не компилируется smoke_test:

  • max должен принимать параметры строго одинаковых типов. Тут лучше кастовать оба аргумента статик кастом к std::ssize_t. Не к long long (0LL), не к int64_t.
      In file included from tests/01_smoke_test.cpp:1:
    ../../../cpp19/morozov.aleksandr/lab_18/include/format.hpp:73:17: error: no matching function for call to 'max'
            1 + 2 * std::max(static_cast<std::int64_t>(helpers_.size()) - 1, 0LL) + 1;
                    ^~~~~~~~
    /usr/lib/llvm-10/bin/../include/c++/v1/algorithm:2633:1: note: candidate template ignored: deduced conflicting types for parameter '_Tp' ('long' vs. 'long long')
    max(const _Tp& __a, const _Tp& __b)
    ^
    /usr/lib/llvm-10/bin/../include/c++/v1/algorithm:2644:1: note: candidate template ignored: could not match 'initializer_list<type-parameter-0-0>' against 'long'
    max(initializer_list<_Tp> __t, _Compare __comp)
    ^
    /usr/lib/llvm-10/bin/../include/c++/v1/algorithm:2653:1: note: candidate function template not viable: requires single argument '__t', but 2 arguments were provided
    max(initializer_list<_Tp> __t)
    ^
    /usr/lib/llvm-10/bin/../include/c++/v1/algorithm:2624:1: note: candidate function template not viable: requires 3 arguments, but 2 were provided
    max(const _Tp& __a, const _Tp& __b, _Compare __comp)
    ^
    
  • Здесь проблема, разобранная на лекции: если хотим SFINAE, то соответствующий кусок должен зависеть от шаблонных параметров самой функции.
    In file included from tests/01_smoke_test.cpp:1:
    ../../../cpp19/morozov.aleksandr/lab_18/include/format.hpp:26:51: error: attempt to use a deleted function
          -> decltype(std::declval<FormatHelper<T>>().estimate_size(), yes());
                                                      ^
    ../../../cpp19/morozov.aleksandr/lab_18/include/format.hpp:35:34: note: in instantiation of template class 'format::HasFormatHelperImpl<(anonymous struct at tests/01_smoke_test.cpp:35:5)>' requested here
    constexpr bool HasFormatHelper = HasFormatHelperImpl<T>::value;
                                     ^
    ../../../cpp19/morozov.aleksandr/lab_18/include/format.hpp:121:18: note: in instantiation of variable template specialization 'format::HasFormatHelper<(anonymous struct at tests/01_smoke_test.cpp:35:5)>' requested here
    std::enable_if_t<HasFormatHelper<T>, std::string> make_string(const T &value) {
                     ^
    tests/01_smoke_test.cpp:20:33: note: while substituting deduced template arguments into function template 'make_string' [with T = (anonymous struct at tests/01_smoke_test.cpp:35:5)]
            -> std::void_t<decltype(format::make_string(value))> {
                                    ^
    /home/osboxes/include/boost/hana/type.hpp:128:13: note: while substituting deduced template arguments into function template 'operator()' [with value:auto = (anonymous struct at tests/01_smoke_test.cpp:35:5)]
                std::declval<F&&>()(std::declval<Args&&>()...)
                ^
    /home/osboxes/include/boost/hana/type.hpp:130:24: note: in instantiation of default argument for 'is_valid_impl<(lambda at tests/01_smoke_test.cpp:19:5) &&, (anonymous struct at tests/01_smoke_test.cpp:35:5) &>' required here
            constexpr auto is_valid_impl(int) { return hana::true_c; }
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /home/osboxes/include/boost/hana/type.hpp:139:22: note: while substituting deduced template arguments into function template 'is_valid_impl' [with F = (lambda at tests/01_smoke_test.cpp:19:5) &&, Args = (no value), $2 = (no value)]
                { return is_valid_impl<F, Args&&...>(int{}); }
                         ^
    tests/01_smoke_test.cpp:36:26: note: in instantiation of function template specialization 'boost::hana::type_detail::is_valid_fun<(lambda at tests/01_smoke_test.cpp:19:5) &&>::operator()<(anonymous struct at tests/01_smoke_test.cpp:35:5) &>' requested here
        CHECK(!is_formattable(s));  // Not static_assert so it's not a compilation error.
                             ^
    /usr/include/doctest.h:2471:15: note: expanded from macro 'CHECK'
    #define CHECK DOCTEST_CHECK
                  ^
    ../../../cpp19/morozov.aleksandr/lab_18/include/format.hpp:15:15: note: 'estimate_size' has been explicitly marked deleted here
      std::size_t estimate_size() const = delete;
                  ^
    
  • В FormatHelper для чисел, строго говоря, resize() может инвалидировать .data(). Нельзя инициализировать first до него.


Стиль 1/3:

  • Вместо yes/no/true_type/false_type следует сразу возвращать constexpr bool из функций.
  • HasFormatHelperImpl не нужен: вместо member detection можно просто расширить FormatHelper<> какой-нибудь статической константой.
  • HasFormatMethodImpl также можно сократить и применить SFINAE прямо в специализации.
  • Не хватает слов final, noexcept
  • vec.push_back(T(10)) --> vec.emplace_back(10) (смотрю на FormatHelper дляв вектора)
  • Вместо push_back для строк следует использовать +=, заодно можно делать += ", "
  • В FormatHelper для чисел:
    • Не проверяется успешность to_chars
  • FORMAT_METHOD_ESTIMATED_SIZE — хорошее название, но следует это сделать статической приватной константой конкретной специализации.
Note: See TracTickets for help on using tickets.