Is it possible to define an alias for a template-template parameter?

痴心易碎 提交于 2019-12-02 08:49:37

问题


I'm experimenting with template-template for fun. I have the following class:

template<template<class> class T, typename R> class Unit
{    
    using FullType = T<R>;
    using Ratio = R;
    //using Type = T;

    ...
};

I have define type R and T<R> as member-types Ratio and FullType. Is it possible to alias T as Type ?

The commented line above give me the following errors on g++ 4.7:

expected nested-name-specifier before 'Type'
using-declaration for non-member at class scope
expected ';' before '=' token
expected unqualified-id before '=' token

I tried some more or less random syntaxes, but none of them compiled.

Thanks !


回答1:


You cannot make an alias for T. The following was discussed in the committee to make an alias for T (because a very late C++11 draft contained notes that stated that it is an alias for T which a Defect Report cleaned up).

// Courtesy of @KerrekSB
template <template <typename> class T, typename R> class Unit
{
    template <typename U> using MyTemplate = T<U>;
    // ...

    // use e.g. MyTemplate<int> to get T<int>
};

Note while MyTemplate<int> is the same type as T<int>, that MyTemplate is not the same as T. The wording at http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1286 was supposed to change that, but in the last meeting it was considered to be a very special machinery that doesn't really fit what alias templates turned out to be (own templates), and it was pushed back to review. To get that effect, a using MyTemplate = T; in future may fit the bill (if proposed and accepted, of course).




回答2:


Since T isn't a type, the question as asked doesn't make sense. However, you can make an alias for T, like:

template <template <typename> class T, typename R> class Unit
{
    template <typename U> using MyTemplate = T<U>;
    // ...

    // use e.g. MyTemplate<int> to get T<int>
};

Pre-C++11 you would need something more notationally involved as outlined in this answer of mine (and used, for example, in the standard library by the standard allocator's rebind mechanic.)



来源:https://stackoverflow.com/questions/13896509/is-it-possible-to-define-an-alias-for-a-template-template-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!