【问题标题】:How can a type alias with using specify a template template argument dependent on a template argument? [duplicate]如何使用 using 指定依赖于模板参数的模板模板参数的类型别名? [复制]
【发布时间】:2015-02-17 10:13:17
【问题描述】:

小例子:

template<template<class ...> class>
struct templ {};

template<class T>
using special = templ<T::type>;

int main() {}

叮当++:

test.cpp:5:23: error: template argument for template template parameter must be a class template or type alias template
using special = templ<T::type>;

确实,我的意思是说T::type是一个类模板,例如

struct detail1 {
    template <class T>
    using type = std::vector<T>;
};

struct detail2 {
    template <class T>
    struct type {};
};

但是怎么能这么说呢? g++ 建议使用typename T::type,但这对我来说是错误的,事实上,这并不能解决错误。

【问题讨论】:

    标签: c++ c++11 templates using template-templates


    【解决方案1】:

    您需要指定type 是一个模板:

    template<class T>
    using special = templ< T::template type>;
    

    这是必需的,因为T::type 依赖于模板参数T

    另见Where and why do I have to put the “template” and “typename” keywords?

    【讨论】:

    • 啊,我见过像::template&lt;&gt; 这样的东西,但这对我来说是新的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 2022-01-14
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 2010-12-17
    • 2018-05-01
    相关资源
    最近更新 更多