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;//这里我应该得到一个错误

cout
static\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
。因此,静态断言永远不会触发。如果您更详细地解释您试图实现的目标,我们可能会建议一种替代方法。