Swarup Sengupta
Swarup Sengupta

Reputation: 63

Passing template class as template template parameter inside class member function

I have a project that needs to be compiled with both GCC-4.4.7 and GCC-4.9.0.

We are using a code that passes a templated class as template template parameter to another class. While the code compiles fine on GCC-4.9.0, it fails on GCC-4.4.7.

Here is a reproduction of the error:

#include <iostream>
using namespace std;

struct E
{
    int a;
    E(int b) : a(b) {}
};

template<template<int B> class C, int D> struct A
{
    void print()
    {
        E e(D);
        cout << e.a << endl;
    }
    int a;
};

template<int B> struct C
{
    const static int a = B;
    void print()
    {
        A<C, B> a;
        a.print();
    }
};

int main() {
    C<3> c;
    c.print();
    return 0;
}

On Compilation:

[swarup@localhost ~]$ g++-4.9 Test.cpp -o Test
[swarup@localhost ~]$ 
[swarup@localhost ~]$ g++-4.4 Test.cpp -o Test
Test.cpp:43: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<int B> class C, int D> struct A’
Test.cpp:43: error:   expected a class template, got ‘C<B>’
Test.cpp:43: error: invalid type in declaration before ‘;’ token
Test.cpp:44: error: request for member ‘print’ in ‘a’, which is of non-class type ‘int’

How to rectify the error and properly get it to compile with GCC-4.4.7?

NB: C++98 standard only, the code is a very old one.

Upvotes: 3

Views: 327

Answers (1)

T.C.
T.C.

Reputation: 137330

Name lookup for C finds the injected class name, and the ancient compiler you are using doesn't support using that as a template name.

Qualify the name.

A< ::C,B> a;

Upvotes: 4

Related Questions