具有从属作用域的嵌套模板 [英] Nested templates with dependent scope

查看:141
本文介绍了具有从属作用域的嵌套模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是依赖作用域,在以下错误的上下文中typename的含义是什么?

What is dependent scope and what is the meaning of typename in the context of the following error?

$ make
g++ -std=gnu++0x main.cpp
main.cpp:18:10: error: need 'typename' before 'ptrModel<std::vector<Data> >::Type' because 'ptrModel<std::vector<Data> >' is a dependent scope
make: *** [all] Error 1


/*
 * main.cpp
 */

#include <vector>
#include <memory>

template<typename T>
struct ptrModel
{
 typedef std::unique_ptr<T> Type;
};


template<typename Data>
struct ptrType
{
 typedef ptrModel< std::vector<Data> >::Type Type;
};

int main()
{
 return 0;
}


推荐答案

去做。在之前写入 typename > ptrModel< std :: vector< Data> > :: Type ,如下所示:

The compiler told you exactly what to do. Write typename before ptrModel<std::vector<Data> >::Type, like so:

 typedef typename ptrModel<std::vector<Data> >::Type Type;

此要求的 reason 是编译器不在此点知道 ptrModel< std :: vector< Data> > :: Type 描述了成员变量或嵌套类型。它甚至不能通过查看 ptrModel 的定义,因为可能有一个专门化 ptrModel for std :: vector< Data> 在程序中的其他地方,它改变了这些东西 :: Type 是指。因此,您需要明确告诉它。

The reason for this requirement is that the compiler doesn't at this point know whether ptrModel<std::vector<Data> >::Type describes a member variable or a nested type. It can't even figure that out by looking at the definition of ptrModel because there might be a specialization of ptrModel for std::vector<Data> somewhere else in the program that it hasn't gotten to yet which changes which of these things ::Type refers to. So you need to tell it explicitly.

名称 ptrModel< std :: vector< Data& > :: Type 有一个dependent scope,因为它在依赖于模板的实例化的范围

The name ptrModel<std::vector<Data> >::Type has a "dependent scope" because it is in a scope that depends on the instantiation of a template.

这篇关于具有从属作用域的嵌套模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆