简体   繁体   中英

template alias in specialized class

The following code gives the error (in line where i define test):

error C2143: syntax error: missing ';' before '<' note: see reference to class template instantiation 'ptc::Produce' being compiled error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Does anybody know why this happens? Compiler is VC2015 CTP1.

Edit: the error must happen in phase1 of the template parsing, because it occurs even I never instantiate the class Produce.

namespace OrderPolicy
{
    struct Unordered {};
    struct Ordered {};
};

template <typename TOrderPolicy>
struct OrderManager {};

template<>
struct OrderManager<OrderPolicy::Unordered>
{
    template <typename TItem>
    using item_t = TItem;
};

template<>
struct OrderManager<OrderPolicy::Ordered> 
{
    template <typename TItem>
    using item_t = TItem*;
};

template<typename TOrderPolicy>
struct Produce : public OrderManager<TOrderPolicy>
{
    item_t<int> test;
    //using item_type = item_t<int>;
};

Edit2: it works when I change the last part of the code to

struct Produce : public OrderManager<OrderPolicy::Ordered>
{
    item_t<int> test;
    //using item_type = item_t<int>;
};

You'll have to use:

typename OrderManager<TOrderPolicy>::template item_t<int>

in place of:

item_t<int> test;
item_t<int> test;

That names a dependent template type from a base class. In this case, you need to tell the compiler both that item_t is a type in your base class, and that it's a template.

The direct way to do this is using typename and template :

typename OrderManager<TOrderPolicy>::template item_t<int> test;

As you can see, this will quickly become unreadable. I would make some local aliases to make the code neater:

using Base = OrderManager<TOrderPolicy>;
using item_type = typename Base::template item_t<int>;
item_type test;

wow, learning never stops. Until now I did not see the keyword template used in this context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM