C++ 为类的特征专门化实现错误消息
当库用户对模板类的模板参数使用错误类型时,如何实现错误消息 test.cpp自适应自 问题:它没有打印我想要的错误消息对不起,非整数类型t的foo尚未实现。将执行以下操作:C++ 为类的特征专门化实现错误消息,c++,templates,template-specialization,sfinae,specialization,C++,Templates,Template Specialization,Sfinae,Specialization,当库用户对模板类的模板参数使用错误类型时,如何实现错误消息 test.cpp自适应自 问题:它没有打印我想要的错误消息对不起,非整数类型t的foo尚未实现。将执行以下操作: template <typename T, typename Enable = void> class foo { static_assert(sizeof(T) == 0, "Sorry, foo<T> for non-integral type T has not been impleme
template <typename T, typename Enable = void>
class foo
{
static_assert(sizeof(T) == 0, "Sorry, foo<T> for non-integral type T has not been implemented");
};
您需要sizeofT==0,因为静态断言总是被计算的,并且需要依赖于T,否则它将总是被触发,即使是对于有效的T。也会这样做:
template <typename T, typename Enable = void>
class foo
{
static_assert(sizeof(T) == 0, "Sorry, foo<T> for non-integral type T has not been implemented");
};
您需要sizeofT==0,因为静态_断言总是被计算的,并且需要依赖于T,否则它将总是被触发,即使对于有效的T也是如此
template <typename T, typename Enable = void>
class foo
{
static_assert(sizeof(T) == 0, "Sorry, foo<T> for non-integral type T has not been implemented");
};