Tools to generate higher-quality error messages for template-based code?

前端 未结 3 1008
暗喜
暗喜 2020-12-13 13:12

Concepts, that would render these tools unnecessary, are not part of C++11.

  • STLFilt would have been one option but it is no longer maintained.

3条回答
  •  清歌不尽
    2020-12-13 13:48

    I have found Clang to generate the best error messages for heavily templated code. Of course verbosity is unavoidable in most cases, but it's still better than GCC or MSVC most of the time. Here's the Clang error message for the example code posted by AshleysBrain:

    $ clang++ -std=c++11 -stdlib=libc++ -o dummy dummy.cpp 
    In file included from dummy.cpp:1:
    In file included from /usr/include/c++/v1/vector:243:
    In file included from /usr/include/c++/v1/__bit_reference:15:
    In file included from /usr/include/c++/v1/algorithm:594:
    /usr/include/c++/v1/memory:1425:36: error: calling a private constructor of class 'std::__1::unique_ptr >'
                    ::new ((void*)__p) _Tp(_STD::forward<_Args>(__args)...);
                                       ^
    /usr/include/c++/v1/memory:1358:14: note: in instantiation of function template specialization
          'std::__1::allocator_traits > >
          >::__construct >, std::__1::unique_ptr > &>' requested here
                {__construct(__has_construct(),
                 ^
    /usr/include/c++/v1/vector:781:25: note: in instantiation of function template specialization
          'std::__1::allocator_traits > >
          >::construct >, std::__1::unique_ptr > &>' requested here
            __alloc_traits::construct(__a, _STD::__to_raw_pointer(this->__end_), *__first);
                            ^
    /usr/include/c++/v1/vector:924:9: note: in instantiation of function template specialization
          'std::__1::vector >,
          std::__1::allocator > >
          >::__construct_at_end > *>' requested here
            __construct_at_end(__x.__begin_, __x.__end_);
            ^
    dummy.cpp:7:37: note: in instantiation of member function 'std::__1::vector >, std::__1::allocator > >
          >::vector' requested here
            std::vector> bar = foo;
                                               ^
    /usr/include/c++/v1/memory:1997:5: note: declared private here
        unique_ptr(const unique_ptr&);
        ^
    1 error generated.
    

    It's still long and ugly, but in my opinion much clearer regarding what/where the problem is.

提交回复
热议问题