开发者

Template typedef of a template [duplicate]

开发者 https://www.devze.com 2023-02-10 07:21 出处:网络
This question already has an answer here: 开发者_StackOverflow中文版 Closed 11 years ago. Possible Duplicate:
This question already has an answer here: 开发者_StackOverflow中文版 Closed 11 years ago.

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.


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>;


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 :-(.

0

精彩评论

暂无评论...
验证码 换一张
取 消