简体   繁体   English

在VS2010中解析/实例化模板有问题吗?

[英]Is it problem with parsing/instantiating templates in VS2010?

Please don't mind the length of this code (just copy and paste). 请不要介意此代码的长度(只需复制和粘贴)。 When you do it run it and it won't compile under VS2010. 当你这样做运行它,它将无法在VS2010下编译。 In order to compile this code, in struct Range remove "class IntType," from template parameters and in main instead of: 为了编译这段代码,在struct Range中从模板参数和main中删除“class IntType”,而不是:

Range<int,float> r;

make 使

Range<float> r; //int is removed 

Code: 码:

template<class T>
struct Assign_Low_High
{
    static const int low_value = 0;
};


//in order to compile remove class IntType, from template params of Range struct
template<class IntType, class L>
struct Range
{
    static_assert(Assign_Low_High<L>::low_value < 1,
                    "Incorrect Range");
};


int main()
{
    //in order to compile remove int from Range
    Range<int,float> r;
    return 0;
}

What on earth is going on? 到底是怎么回事? (it does compile with GCC 4.5.1). (它确实与GCC 4.5.1一起编译)。

Well it looks like the < operator throws the compiler off the wrong track. 好吧,看起来<运算符会使编译器偏离错误的轨道。 If you: 如果你:

static_assert( Assign_Low_High<L>::low_value > -1, "Incorrect Range");

or 要么

static_assert( (Assign_Low_High<L>::low_value) < 1, "Incorrect Range");

it will work. 它会工作。

If you do: 如果你这样做:

static_assert( Assign_Low_High<L>::low_value < 1 > 0, "Incorrect Range");

then it gets interesting... 然后它变得有趣......

I think the compiler should consider the low_value dependent name to be a non type non template dependent name and consider the "<" following low_value to be the less than operator. 我认为编译器应该将low_value依赖名称视为非类型非模板依赖名称,并将low_value后面的“<”视为小于运算符。 So I would say the gcc compiler does the right thing while the MS 2010 compiler does not, but fortunately it can be helped to produce the desired effect. 所以我会说gcc编译器做正确的事情,而MS 2010编译器没有,但幸运的是它可以帮助产生所需的效果。

One more thing, this is obviously not due to the static_assert since: 还有一件事,这显然不是由于static_assert,因为:

 bool bComp = Assign_Low_High<int>::low_value < 1;

directly in main leads to the same compile errors... 直接在主要导致相同的编译错误...

I can't compile that right now, but it looks like its your static_assert thats failing. 我现在无法编译,但它看起来像你的static_assert失败了。 You're asserting that low value is less than 0, when it is actually equal to 0. 当你实际上等于0时,你断言低值小于0。

If thats not the problem, would you mind posting the compiler error ? 如果那不是问题,你介意发布编译错误吗?

EDIT Moving the static assert to the constructor of Range compiles properly, and still accomplishes the goal of asserting whenever you use the template. 编辑将静态断言移动到Range的构造函数正确编译,并且在您使用模板时仍然可以实现断言的目标。 Based on this, and the fact that the clause in the assert compiles when not in an assert, I would assume that MS's static_assert implementation is a bit buggy. 基于此,以及断言中的子句在不在断言时编译的事实,我会假设MS的static_assert实现有点错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM