为了好玩,我正在试验模板模板。我有以下类(class):
template<template<class> class T, typename R> class Unit
{
using FullType = T<R>;
using Ratio = R;
//using Type = T;
...
};
我定义了类型 R
和 T<R>
作为成员类型 Ratio
和 FullType
.
是否可以使用别名 T
作为Type
?
上面的注释行在 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
我尝试了一些或多或少的随机语法,但没有一个被编译。
谢谢!
最佳答案
因为 T
不是类型,所以所问的问题没有意义。但是,您可以为 T
创建一个别名,例如:
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>
};
C++11 之前的版本,您需要更多符号化的内容,如 in this answer of mine 所述(例如,标准分配器的 rebind
机制在标准库中使用。)
关于c++ - 是否可以为模板模板参数定义别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13896509/