Problem with C ++ pattern in cross-platform code

I am having trouble compiling this code on Linux, but it works fine on Windows.

Windows Compiler: Visual Studio 2005

Linux compiler: gcc version 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)

class DoSomething
{
  public:
    template <class DataType>
    bool Execute()
    {
       //do something here
    }
};


template <class Operator>
TypeSwitch(int DataTypeCode, Operator& Op)
{
   switch (DataTypeCode)
   {
     case 1: return Op.Execute<char>();
     case 2: return Op.Execute<int>();
     //snip;
   }
}  

//To call the operator
TypeSwitch(Code,DoSomething);

On Windows, this code works fine and does exactly what I want. On Linux, I get errors:

error: expected primary expression before the token '>'

error: expected primary expression before ')' token

for each of the lines with the case statement.

Any ideas?

Thanks Mike

+5
source share
2 answers

The problem is that when the compiler encounters Op.Execute<char>();and tries to parse it, it gets confused.

Op - , . , Execute . , < . , - Execute - .

, :

case 1: return Op.template Execute<char>();

, Execute , , <, "", .

, typename , . - , template.

GCC , MSVC . template, ( )

+13
 case 1: return Op.template Execute<char>();
 case 2: return Op.template Execute<int>();

: template

, TypeSwitch() bool

+1

All Articles