专用类中的模板别名
template alias in specialized class
下面的代码给出了错误(在我定义测试的行中):
错误C2143:语法错误:缺少";"在'<'之前注意:请参阅对正在编译的类模板实例化"ptc::Produce"的引用错误C4430:缺少类型说明符-假定为int。注意:C++不支持默认的int
有人知道为什么会发生这种事吗?编译器是VC2015 CTP1。
编辑:错误必须发生在模板解析的第1阶段,因为即使我从未实例化类Produce也会发生。
namespace OrderPolicy
{
struct Unordered {};
struct Ordered {};
};
template <typename TOrderPolicy>
struct OrderManager {};
template<>
struct OrderManager<OrderPolicy::Unordered>
{
template <typename TItem>
using item_t = TItem;
};
template<>
struct OrderManager<OrderPolicy::Ordered>
{
template <typename TItem>
using item_t = TItem*;
};
template<typename TOrderPolicy>
struct Produce : public OrderManager<TOrderPolicy>
{
item_t<int> test;
//using item_type = item_t<int>;
};
第2版:当我将代码的最后一部分更改为时,它就起作用了
struct Produce : public OrderManager<OrderPolicy::Ordered>
{
item_t<int> test;
//using item_type = item_t<int>;
};
item_t<int> test;
它从基类中命名一个依赖模板类型。在这种情况下,您需要告诉编译器item_t
是基类中的一个类型,并且它是一个模板。
直接的方法是使用typename
和template
:
typename OrderManager<TOrderPolicy>::template item_t<int> test;
正如你所看到的,这将很快变得不可读。我会制作一些本地别名以使代码更整洁:
using Base = OrderManager<TOrderPolicy>;
using item_type = typename Base::template item_t<int>;
item_type test;
您必须使用:
typename OrderManager<TOrderPolicy>::template item_t<int>
代替:
item_t<int> test;
哇,学习从未停止。直到现在,我还没有看到在这个上下文中使用的关键字模板。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 部分定义/别名模板模板参数
- 调用专用模板时出错"no matching function for call to [...]"
- 如何在C++20中创建模板别名的推导指南
- 模板专用化(按容器):value_type
- MSVC:带函数的"error C2244: unable to match function definition to an existing declaration"是指专用模板类的类型别名
- 为什么模板别名专用化取决于引用它的上下文
- 如何检查函数中的模板参数是否与给定类型别名的专用化匹配
- 如何使用类模板专用化创建别名模板的专用化
- 类模板专用化中的成员变量别名
- 模板别名可以用于部分专用化吗
- C++11中一个专用模板的名称别名
- 别名模板专用化问题
- 别名模板、部分专用化和无效参数类型无效
- 如何为模板专用化设置别名
- 专用模板是命名空间别名实际上是一个别名
- 专用类中的模板别名
- 使用完全专用的别名进行实现切换
- 函数模板专用化的别名
- 关于自定义分配器和 stl 的模板声明、别名和专用化的说明