从模板实例化/类型推导中查找错误消息的实际来源(Finding the actual source for an error message from a template instantiation

系统教程 行业动态 更新时间:2024-06-14 16:58:00
从模板实例化/类型推导中查找错误消息的实际来源(Finding the actual source for an error message from a template instantiation / type deduction)

我正在尝试构建的某些第三方库的模板化函数中出现错误。

MSVC指向该功能并告诉我,我正在为特定呼叫做错事。 我怎么知道错误发生在哪个电话?


如果重要,这就是功能:

template <typename T> std::string ToString(T number) { std::ostringstream ss; ss << std::setprecision(NUM_TO_STRING_PRECISION); ss << number; return ss.str(); }

错误是:

错误C2088'<<':类非法

I am getting some error in a templated function of some 3rd party library that I am trying to build.

MSVC points to the function and told me that I am doing something wrong for a specific call. How can I know at which call exactly the error happens?


If it matters, this is the function:

template <typename T> std::string ToString(T number) { std::ostringstream ss; ss << std::setprecision(NUM_TO_STRING_PRECISION); ss << number; return ss.str(); }

The error is:

Error C2088 '<<': illegal for class

最满意答案

我怎么知道错误发生在哪个电话?

对于visual-studio来说并不是真的,但让我们看一个简单的例子 :

#include <vector> struct Foo { Foo() = delete; }; int main() { std::vector<Foo> vfoo(15); (void)vfoo; }

GCC g ++输出以下错误消息:

In file included from /usr/local/include/c++/7.1.0/vector:63:0, from main.cpp:1: /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h: In instantiation of 'static _ForwardIterator std::__uninitialized_default_n_1<true>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Foo*; _Size = long unsigned int]': /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h:583:20: required from '_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Foo*; _Size = long unsigned int]' /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h:645:44: required from '_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = Foo*; _Size = long unsigned int; _Tp = Foo]' /usr/local/include/c++/7.1.0/bits/stl_vector.h:1347:36: required from 'void std::vector<_Tp, _Alloc>::_M_default_initialize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = Foo; _Alloc = std::allocator<Foo>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]' /usr/local/include/c++/7.1.0/bits/stl_vector.h:285:30: required from 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = Foo; _Alloc = std::allocator<Foo>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<Foo>]' main.cpp:9:29: required from here /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h:548:37: error: use of deleted function 'Foo::Foo()' return std::fill_n(__first, __n, _ValueType()); ^~~~~~~~~~~~ main.cpp:4:5: note: declared here Foo() = delete; ^~~

IIRC在视觉工作室非常相似。 只需打开原始输出选项卡,然后转到最后一个note ,该note可能包含错误的真正来源


使用rextester进行 MSVC结果。 同样, source_file.cpp最后一个注释指向了错误的真正来源。

How can I know at which call exactly the error happens?

Not really with visual-studio, but let's look at a simple example:

#include <vector> struct Foo { Foo() = delete; }; int main() { std::vector<Foo> vfoo(15); (void)vfoo; }

GCC g++ outputs these error messages:

In file included from /usr/local/include/c++/7.1.0/vector:63:0, from main.cpp:1: /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h: In instantiation of 'static _ForwardIterator std::__uninitialized_default_n_1<true>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Foo*; _Size = long unsigned int]': /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h:583:20: required from '_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Foo*; _Size = long unsigned int]' /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h:645:44: required from '_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = Foo*; _Size = long unsigned int; _Tp = Foo]' /usr/local/include/c++/7.1.0/bits/stl_vector.h:1347:36: required from 'void std::vector<_Tp, _Alloc>::_M_default_initialize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = Foo; _Alloc = std::allocator<Foo>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]' /usr/local/include/c++/7.1.0/bits/stl_vector.h:285:30: required from 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = Foo; _Alloc = std::allocator<Foo>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<Foo>]' main.cpp:9:29: required from here /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h:548:37: error: use of deleted function 'Foo::Foo()' return std::fill_n(__first, __n, _ValueType()); ^~~~~~~~~~~~ main.cpp:4:5: note: declared here Foo() = delete; ^~~

IIRC it's very similar in visual studio. Just open the raw output tab, and go to the very last note, which probably contains the real source of the error.


MSVC results with rextester. Again the very last notes for source_file.cpp point to the real source of the error.

更多推荐

本文发布于:2023-04-14 05:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/9fd81bb11e01db92209e4eb7b1949f52.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   模板   错误   来源   消息

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!