其实我只是找到了答案,以我自己的问题:
template<typename T>
void show_type_abort_helper()
{
return __PRETTY_FUNCTION__;
}
#define show_type_abort(x) show_type_abort_helper< decltype(x) >()
用法:
std::vector<int> s{1, 2, 3};
for (auto elem : s) {
show_type_abort(elem);
elem = 5;
}
产生一个与g++
以下错误消息(6.1.1版本):
$ g++ test.cpp
test.cpp: In instantiation of ‘void show_type_abort_helper() [with T = int]’:
^^^
test.cpp:17:9: required from here
test.cpp:7:12: error: return-statement with a value, in function returning 'void' [-fpermissive]
return __PRETTY_FUNCTION__;
检查输出T = int
以查看编译器使用int
作为类型。这似乎与铛工作,以及:
$ clang++-3.8 -std=c++11 test.cpp
test.cpp:7:5: error: void function 'show_type_abort_helper' should not return a value [-Wreturn-type]
return __PRETTY_FUNCTION__;
^ ~~~~~~~~~~~~~~~~~~~
test.cpp:17:9: note: in instantiation of function template specialization 'show_type_abort_helper<int>' requested here
^^^
show_type_abort(elem);
更改为for (const auto& elem : s)
给
with T = const int&
^^^^^^^^^^
或
show_type_abort_helper<const int &>
^^^^^^^^^^^
这样看来我可以找出在编译时的类型和中止。这对于一个非常复杂的类型来说非常方便,它由几个typedef和模板参数组成,我只是无法看到发生了什么。
不知道你在问什么。你要写的任何代码确保'elem'是一个引用,看起来比写'auto && elem'开始的时间要长。 – Barry
您可以使用['std :: is_reference'](http://en.cppreference.com/w/cpp/types/is_reference)和['static_assert'](http://en.cppreference.com/w/CPP /语言/ static_assert)。但是它补充说,对于每一个循环来说,更多的工作不仅仅是确保使用正确的类型来开始。不,没有办法让编译器为你做检查,因为这个任务不是无效的。 –
您的示例看起来是编译器警告的好理由。例如,我的编译器说'警告:变量'elem'设置但未使用'。 – Sergey