开发者

Template typedef of a template [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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜