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. In order to compile this code, in struct Range remove "class IntType," from template p开发者_开发百科arameters and in main instead of:
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).
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. 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.
One more thing, this is obviously not due to the static_assert since:
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. You're asserting that low value is less than 0, when it is actually equal to 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. 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.
精彩评论