gpt4 book ai didi

c++ - typedef 为模板模板的别名

转载 作者:搜寻专家 更新时间:2023-10-31 00:10:13 24 4
gpt4 key购买 nike

我有一个特征类 AttributeTraits,它使用智能指针模板类 MySmartPointer:

template<typename T>
class MySmartPointer {};

template<template <typename> class SP = MySmartPointer>
struct AttributeTraits {
template <typename T>
using SmartPointerClass = SP<T>;
};

AttributeTraits 使用别名来保存它拥有的模板类。现在,我需要获取特征类的属性类将使用智能指针类的类型定义/别名,例如:

template <typename Traits>
struct Attribute {
typedef typename Traits::SmartPointerClass<int> SmartPointerType; // error: non-template 'SmartPointerClass' used as template
using SmartPointerType2 = typename Traits::SmartPointerClass<int>; // error: expected ';' before '<' token
using SmartPointerType3 = Traits::SmartPointerClass<int>; // error: non-template 'SmartPointerClass' used as template
};

但是上面的 3 个选项都不起作用...

最佳答案

这是你的依赖类是模板的时候之一,所以你必须在定义你的别名时使用笨拙的 template 语法:

template <typename Traits>
struct Attribute {
typedef typename Traits::template SmartPointerClass<int> SmartPointerType;
using SmartPointerType2 = typename Traits::template SmartPointerClass<int>;
using SmartPointerType3 = typename Traits::template SmartPointerClass<int>;
};

参见 this documentation有关何时需要 template 关键字的说明:

The template disambiguator for dependent names
Similarly, in a template definition, a dependent name that is not a member of the current instantiation is not considered to be a template name unless the disambiguation keyword template is used or unless it was already established as a template name

This StackOverflow Answer也提供了一些很好的背景。

关于c++ - typedef 为模板模板的别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39209037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com