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

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

为了好玩,我正在试验模板模板。我有以下类(class):

template<template<class> class T, typename R> class Unit
{    
    using FullType = T<R>;
    using Ratio = R;
    //using Type = T;

    ...
};

我定义了类型 RT<R>作为成员类型 RatioFullType . 是否可以使用别名 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/

相关文章:

c++ - 使用模板时出现编译错误

c++ - 如何编写同时接受 && 和 const& 的模板函数?

C++ 模板变量和参数包扩展

c++ - 枚举作为函数参数

python - 将代码的基本部分从 C++ 转换为 Python

c++ - 为什么我不能有模板和默认参数?

c++ - 为什么我收到协议(protocol)不支持的地址族错误?

c++ - 这个数组大小模板是如何工作的?

c++ - 嵌套的 unique_ptr 和 STL 容器

前向声明类的 unique_ptr 的 C++11 容器