tags:

views:

43

answers:

2

The following code

template<typename T, typename U> class Alpha
{
public:
    template<typename V> void foo() {}
};

template<typename T, typename U> class Beta
{
public:
    Alpha<T, U> alpha;
    void arf();
};

template<typename T, typename U> void Beta<T, U>::arf()
{
    alpha.foo<int>();
}

int main()
{
    Beta<int, float> beta;
    beta.arf();
    return 0;
}

Fails to compile due to:

../src/main.cpp: In member function ‘void Beta::arf()’:
../src/main.cpp:16: error: expected primary-expression before ‘int’
../src/main.cpp:16: error: expected ‘;’ before ‘int’

How the heck do I fix this? I've tried everything I can think of.

+4  A: 

Try alpha.template foo<int>(). Note that your code compiles fine with VC8. Since alpha is of dependant type, you have to specify that foo is a template.

Alexandre C.
+4  A: 

alpha::foo is a dependent name, use alpha.template foo<int>().

Dependent names are assumed to

  • not be types unless prefixed by typename
  • not be templates unless directly prefixed by template
Georg Fritzsche
VC8 is not standard regarding this point. Writing portable code with VC8 is such a mess...
Alexandre C.
@ale: Oh yes - and porting template-heavy parts later from VC8 is quite a pain.
Georg Fritzsche