使用的类型别名如何指定依赖于模板参数的模板模板参数

How can a type alias with using specify a template template argument dependent on a template argument?

本文关键字:参数 依赖于 何指定 类型 别名      更新时间:2023-10-16

最小示例:

template<template<class ...> class>
struct templ {};
template<class T>
using special = templ<T::type>;
int main() {}

clang++:

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,但这对我来说是错误的,事实上,这并不能解决错误。

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

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

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

另请参阅"template"answers"typename"关键字的位置和原因?