【发布时间】:2016-11-07 19:07:43
【问题描述】:
当库用户对模板类的模板参数使用错误类型时,如何实现错误消息?
test.cpp(改编自here)
#include <type_traits>
template <typename T, typename Enable = void>
class foo; // Sorry, foo<T> for non-integral type T has not been implemented.
template <typename T>
class foo<T, typename std::enable_if<std::is_integral<T>::value>::type>
{ };
int main()
{
foo<float> x;
}
代码没有按预期编译。但是我不能让编译器只在用户使用错误类型时才显示错误。
g++ test.cpp的错误信息
test.cpp: In function ‘int main()’:
test.cpp:11:13: error: aggregate ‘foo<float> x’ has incomplete type and cannot be defined
foo<float> x;
问题:它没有打印出我想要的错误消息 (Sorry, foo<T> for non-integral type T has not been implemented.)
【问题讨论】:
标签: c++ templates template-specialization sfinae specialization