0

Let's suppose I have two classes (actually more, but only 2 in my MCVE) defining two static functions with exactly the same name:

class A
{
public:
    static void doSomething() {};
    static void doSomethingElse() {};
};

class B
{
public:
    static void doSomething() {};
    static void doSomethingElse() {};
};

I want to call one of those functions for all available classes. So I created a helper function:

template<class Helper> static void ApplyToAllTypes( Helper& helper )
{
    helper.apply<A>();
    helper.apply<B>();
}

Then I do this to call doSomething on all classes:

class doSomethingHelper
{
public:
    template<class T> static void apply()
    {
        T::doSomething();
    }
};

void doSomethingToAll()
{
    doSomethingHelper helper;
    ApplyToAllTypes<doSomethingHelper>( helper );
}

And this to call doSomethingElse on all classes:

class doSomethingElseHelper
{
public:
    template<class T> static void apply()
    {
        T::doSomethingElse();
    }
};

void doSomethingElseToAll()
{
    doSomethingElseHelper helper;
    ApplyToAllTypes<doSomethingElseHelper>( helper );
}

It works fine when compiled with MSVC, but when I try to compile this with g++, it complains:

In static member function 'static void ApplyToAllTypes()':
 error: expected '(' before '>' token
         helper.apply<A>();

Is that really invalid? Should the sytax be fixed in any way or do I need to find an alternative (then proposed alternative would be appreciated)?

jpo38
  • 19,174
  • 7
  • 62
  • 118

1 Answers1

1

You have to write

helper.template apply<A>();

Visual Studio accepts this (wrong) syntax, though.

Jodocus
  • 6,404
  • 1
  • 22
  • 38