Is this a problem with parsing / creating templates in VS2010?

Please ignore the length of this code (just copy and paste). When you run it, it will not compile under VS2010. To compile this code, in struct Range, remove the "class IntType" from the template parameters and in the main, and not:

Range<int,float> r; 

to do

 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's happening? (It compiles with GCC 4.5.1).

+4
source share
2 answers

Well, it looks like the <statement kicks 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"); 

he will work.

If you do:

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

then it becomes interesting ...

I think that the compiler should consider the low_value dependent name as a type-independent name and not consider the type “<” below the low_value will be less than the operator. Therefore, I would say that the gcc compiler does the right thing until the MS 2010 compiler does this, but, fortunately, this can help get the desired effect.

One more thing, this is obviously not due to static_assert, because:

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

directly in the main leads to the same compilation errors ...

+4
source

I cannot compile this right now, but it looks like your static_assert is not working. You state that a low value is less than 0 when it is actually 0.

If this is not a problem, could you post a compiler error?

EDIT Moving a static statement into the Range constructor compiles properly and still performs the approval task when using the template. Based on this and the fact that the sentence in the assert command compiles when not in assert, I would assume that the MS static_assert implementation is a bit wrong.

0
source

Source: https://habr.com/ru/post/1346358/


All Articles