Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template typedef of a template [duplicate]

Tags:

c++

templates

Possible Duplicate:
C++ template typedef

I am trying to derive a template type of another template by pre-specializing of another template:

template<unsigned a, unsigned b, unsigned c>
struct test
{
    enum
    {
        TEST_X = a,
        TEST_Y = b,
        TEST_Z = c,
    };
};

template<unsigned c>
typedef test<0, 1, c> test01;

However, on GCC 4.4.5, I am getting this error: error: template declaration of ‘typedef’ on the second type (test01).

Guidance would be highly appreciated, as I don't understand what is wrong with my code.

like image 977
LiraNuna Avatar asked Feb 15 '11 08:02

LiraNuna


2 Answers

This syntax isn't allowed by C++03. The nearest work-around is:

template<unsigned c>
struct test01
{
    typedef test<0, 1, c> type;
};

typedef test01<2>::type my_type;

In C++0x, we can do this:

template<unsigned c>
using test01 = test<0, 1, c>;
like image 105
GManNickG Avatar answered Sep 28 '22 02:09

GManNickG


Just for the sake of listing an alternative:

template <typename C>
struct test01 : test<0, 1, C> { };

test01<4> my_test014;

This does create new, distinct types and not simply aliases for instantiations of the base template :-(.

like image 39
Tony Delroy Avatar answered Sep 28 '22 03:09

Tony Delroy