作为模板参数 c++ 给出的类的别名模板
Alias template of a class given as a template parameter c++
如何将类的别名模板A
作为模板参数提供给从模板基类B
继承的类C
?
#include <vector>
struct A
{
// the alias template I want to refer to:
template<class T>
using Container = std::vector<T>;
};
// the base class
template<template<class> class _Container>
struct B
{
_Container<int> m_container;
};
template<class _A>
struct C : public B< typename _A::Container >
{// ^^^^^^^^^^^^^^^^^^^^^^
};
int main()
{
C<A> foo;
}
我通过在语句中的每个可能位置添加template
关键字(如template<class T> typename _A::Container<T>
、typename _A::template Container
...(尝试了几种解决方案,但g++
给出了"模板参数 1 无效">或"类型/值不匹配">!
正确的语法是:
template <class A>
struct C : public B< A::template Container >
{
};
住
顺便说一句:不要使用_A
作为模板参数的名称,C++保留以下划线开头后跟大写字母的标识符。
相关文章:
- 部分定义/别名模板模板参数
- 如何在C++20中创建模板别名的推导指南
- 告诉c++编译器该参数没有别名
- boost::spirit::karma 替代生成器,带有 boost::variant 由字符串和字符串别名组成
- 继承模板类中的类型别名
- 别名模板的专业化 C++11 中没有开销的最佳替代方案
- 为什么 GCC 在使用类型别名时处理 const reinterpret_cast不同?
- 类作用域的类型别名"using":[何时]方法中的用法可以先于类型别名?
- 为什么我们不能重复使用具有不同模板参数的别名模板标识符?
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- 如何使用类型别名从模板化类中隐藏模板列表
- 模板模板参数和模板别名:编译器错误?
- 使用定义函数模板别名
- 为模板参数包添加别名
- 如何检测类型类型别名?
- C++类型别名,其中值被替换
- C++使用默认模板参数键入别名和转发声明
- 在Qt中注册自定义元类型的别名类型
- 缺少别名模板C++参数列表
- 使用外部定义的模板类型作为模板参数的更通用模板的模板别名