0

Hello I'm currently analyzing a source code below which works well in Linux (MinGW) but doesn't work in MSVC, visual studio in Windows. This code is following C++ 03 rule, not C++ 11.

template <typename T>
class CTestClass
{
 public:
 static const int Width = T::Length;
 static const typename T::TestType begin = T::begin;
 static const typename T::TestType end = T::end;
};

This is the error message in visual studio 2017 (MSVC).

error C2510: 'T': left of '::' must be a class/struct/union
error C2065: 'Length': undeclared identifier
error C2065: 'begin': undeclared identifier

I can see the intention of the code : T can be any class which has static const member variables Length, begin, and end. But the way of expressing is quite awkward to me. From my understanding, this should not work cause T is not supposed to be defined as typename T (but class T), also There should be T instance declared inside CTestClass.. But still this works in Linux somehow.

I wonder

  1. How this grammar works in Linux(MinGW) but not in MSVC?
  2. To make this works in Visual Studio in Windows (MSVC compiler), how should I modify that CTestClass ?
  • 1
    Can we get a [mre]? [code works fine here](http://coliru.stacked-crooked.com/a/3d9ebb5792bc4e16) – NathanOliver Sep 20 '21 at 12:43
  • Also, when you say *T can be any class or namespace*, that is incorrect. Templates deal with types or values (non-type template parameter), not namespaces. If you try to pass a namespace to T you will get an error. – NathanOliver Sep 20 '21 at 12:45
  • @NathanOliver yes, T is used as class in the source code. I removed the part mentioning namespace. Thank you. – juyeon hong Sep 20 '21 at 13:01
  • @NathanOliver your example code is exactly what the source code does. I copied your code into visual studio project, and got the same result ( error C2825: 'T': must be a class or namespace when followed by '::' , etc) your code will work in Linux tho. I wonder why this grammar is not allowed in MSVC. – juyeon hong Sep 20 '21 at 13:07
  • Nathan's example compiles without error in my copy of MSVC 2019 – Igor Tandetnik Sep 20 '21 at 15:30
  • @IgorTandetnik Thank you.. Is there any project setting I should activate or deactivate to make it work in MSVC, or change coding rule? – juyeon hong Sep 21 '21 at 00:50
  • MinGW is not Linux. – ssbssa Sep 21 '21 at 10:46
  • In Nathan's example, you used constexpr, but I should not use this as this is introduced after ISO C++11. the source code is following C++03. As I am using MSVC2017, there is no way to force language mode into C++03. I think that's the reason it doesn't work in Visual Studio 2017. I wonder if there is any way I can change the coding rule into C++03 in Visual Studio 2017.. – juyeon hong Sep 21 '21 at 11:44

0 Answers0