The*_* do -8 c++ static-assert decltype c++11
为什么类定义中的 this (static_assert) 不起作用?
template<class IntT, IntT low = IntT(), IntT high = IntT()>
struct X
{
static_assert(std::is_same<decltype(low),decltype(high)>::value,"Different types not allowed");
};
int _tmain(int argc, _TCHAR* argv[])
{
int low, high;
X<char,1,'a'> x;//HERE I SHOULD GET ERROR
cout << sizeof(x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
static_assert工作正常,是你的代码从不断言。
模板struct X定义low和highas 类型IntT。无论它们具有什么值,它们都是相同的类型。
当您实例化 struct ( X<char,1,'a'> x) 时,您是在告诉编译器类型IntT是char并且正在赋予low该值1和high该值'a'(即 97)。但是,lowand的类型high总是char如此,static_assert永远不会断言。
| 归档时间: |
|
| 查看次数: |
2357 次 |
| 最近记录: |