且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以为模板模板参数定义别名?

更新时间:2023-11-30 19:48:58

您不能为T创建别名.在委员会中讨论了以下内容,以为T作别名(因为最近的C ++ 11草案包含说明 T的别名,而缺陷报告已清理).

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

请注意,虽然MyTemplate<int>T<int>是同一类型,但MyTemplateT相同. http://www.open-std上的措辞. org/jtc1/sc22/wg21/docs/cwg_active.html#1286 应该进行更改,但是在上次会议中,它被认为是一种非常特殊的机制,与别名模板的结果完全不符成为(自己的模板),并将其推回审查.为了达到这种效果,将来的using MyTemplate = T;可能会符合要求(当然,如果提出并接受的话).

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).