C++ 静态断言中的decltype
为什么类定义中的这个(静态_断言)不起作用C++ 静态断言中的decltype,c++,c++11,decltype,static-assert,C++,C++11,Decltype,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[]) {
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;
}
模板
结构X
{
静态断言(std::is_same::value,“不允许不同类型”);
};
int _tmain(int argc,_TCHAR*argv[]
{
int低,高;
X;//这里我应该得到一个错误
coutstatic\u assert
工作正常,您的代码从不断言。
模板struct X
将low
和high
定义为IntT
类型。它们都是相同的类型,不管它们有什么值。
当你实例化结构(X
)时,你告诉编译器IntT
的类型是char
,并且给low
值1
和high
值'a'
(即97)但是,low
和high
的类型总是char
,因此static\u assert
永远不会断言。什么是“low”和“high”?还有,你会遇到什么错误?被否决:不完整——缺少关于low
和high
是什么的信息——甚至不关心回答t的请求他是愿意帮助的人。@David我特别没有提供关于什么是低和高的信息,因为这个构造没有(或似乎没有)做任何类型的工作。但是更新了我的答案。@David这既不是恶作剧也不是玩笑。我不确定你的态度是从哪里来的,但它不是最健康的。因为low
被声明为IntT low
和high
作为IntT high
,decltype(low)
和decltype(high)
的定义是IntT
。因此,静态断言永远不会触发。如果您更详细地解释您试图实现的目标,我们可能会建议一种替代方法。