c++ - 等同于 "typename",表示从属名称确实是 'template template parameter'

标签 c++ templates c++11 dependent-name

我们将无法找到正确语法的部分代码缩减为最小示例。

让我们假设以下定义(不用担心“为什么”;)

template <class>
class Element
{};

template <template <class> class>
class Client
{};

template <class>
struct TemplatedProvider
{
    template <class T>
    using element_template = Element<T>;
};

现在,从 C++11 开始,我们可以使用类模板或类型别名模板来实例化 Client 模板。以下函数编译得很好:

void fun()
{
    Client<Provider::element_template> client;
    Client<TemplatedProvider<int>::element_template> clientBis;
}

但在以下情况下,当给 Client 的模板参数是一个从属名称时,我们找不到正确的语法:

template <class T>
void templatedFun()
{
    Client<TemplatedProvider<T>::element_template> client;
}

Clang(使用 3.6 测试)发出以下编译错误:

template argument for template template parameter must be a class template or type alias template

我们可以修正这个语法吗?

最佳答案

它必须是:

template <class T>
void templatedFun()
{
    Client<TemplatedProvider<T>::template element_template> client;
}

关于c++ - 等同于 "typename",表示从属名称确实是 'template template parameter',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33417989/

相关文章:

c++ - 如何将 double 转换为两位小数?

c++ - 从使用 Wix 制作的 MSI 生成文本

c++ - 在enable_if_t中调用constexpr函数

c++ - 带有模板基类的静态成员定义

c++ - 使用 std::allocator 和 std::move 防止释放的正确方法

c++ - 为什么表达式 (int) +1e10 不会产生 -2147483648 正如 CSAPP 所描述的那样?

java - SMTP c++ 客户端

c++ - 如何避免 typedef?

c++ - 为什么将条件写入转换为无条件写入不是线程安全优化?

c++ - 是否有 move const 对象的有用场景?