从依赖基类访问类型 [英] Accessing types from dependent base classes

查看:170
本文介绍了从依赖基类访问类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么使用声明似乎不能从依赖基类导入类型名称吗?他们为成员变量和函数工作,但至少在GCC 4.3中,它们似乎被类型忽略。

Does anyone know why using-declarations don't seem to work for importing type names from dependent base classes? They work for member variables and functions, but at least in GCC 4.3, they seem to be ignored for types.

template <class T>
struct Base
{
  typedef T value_type;
};

template <class T>
struct Derived : Base<T>
{
  // Version 1: error on conforming compilers
  value_type get();

  // Version 2: OK, but unwieldy for repeated references
  typename Base<T>::value_type get();

  // Version 3: OK, but unwieldy for many types or deep inheritance
  typedef typename Base<T>::value_type value_type;
  value_type get();

  // Version 4: why doesn't this work?
  using typename Base<T>::value_type;
  value_type get(); // GCC: `value_type' is not a type
};

我有一个基类和一组分配器样式的typedef,我想继承几个层次的继承。我发现到目前为止的最佳解决方案是上面的版本3,但我很好奇为什么版本4似乎不工作。 GCC接受使用声明,但似乎忽略它。

I have a base class with a set of allocator-style typedefs that I'd like to inherit throughout several levels of inheritance. The best solution I've found so far is Version 3 above, but I'm curious why Version 4 doesn't seem to work. GCC accepts the using-declaration, but seems to ignore it.

我检查了C ++标准,C ++ Prog。郎。第3版。 [Stroustrup]和C ++模板[Vandevoorde,Josuttis],但没有一个解决了使用声明是否可以应用于依赖的基类类型。

I've checked the C++ Standard, C++ Prog. Lang. 3rd ed. [Stroustrup], and C++ Templates [Vandevoorde, Josuttis], but none seem to address whether using-declarations can be applied to dependent base class types.

看到另一个例子,这里是同样的问题,但不是真的回答,在GCC邮件列表上。 asker表示他在其他地方看到使用typename,但是GCC似乎不支持它。我没有另一个合格的编译器可以测试它。

In case it helps to see another example, here is the same question being asked, but not really answered, on the GCC mailing list. The asker indicates that he has seen 'using typename' elsewhere, but that GCC doesn't seem to support it. I don't have another conforming compiler available to test it.

推荐答案

正如Richard Corden指出的,在批准了2003年标准后, C ++标准核心语言缺陷报告关键字typename / template如何与使用声明进行交互?

As Richard Corden points out, this issue was addressed in the C++ Standard Core Language Defect Reports after the 2003 standard was ratified: How do the keywords typename/template interact with using-declarations?


建议的决议(2003年4月,
,2003年10月修订):

Proposed resolution (April 2003, revised October 2003):

新段落到
底部7.3.3 [namespace.udecl]:

Add a new paragraph to the bottom of 7.3.3 [namespace.udecl]:

如果一个using声明使用
关键字typename并指定一个
从属名称(14.7.2 [temp.dep]),

使用声明引入的名称被视为
typedef-name(7.1.3 [dcl .typedef])。

If a using-declaration uses the keyword typename and specifies a dependent name (14.7.2 [temp.dep]), the name introduced by the using-declaration is treated as a typedef-name (7.1.3 [dcl.typedef]).

这个文本似乎没有出现在2003年10月15日的第二版标准中。

This text doesn't seem to appear in the Second Edition standard from October 15, 2003.

GCC尚未实现此解决方案,如错误中所述14258

GCC does not yet implement this resolution, as explained in bug 14258:


-------评论#3来自Giovanni Bajo 2004-02-27 12:47 [回复] -------
的问题是我们的USING_DECL不是
记录typename,也就是
的事实,它是一个类型是
通过它进口。

------- Comment #3 From Giovanni Bajo 2004-02-27 12:47 [reply] ------- The problem is that our USING_DECL doesn't record the "typename", that is the fact that it is a type which is imported through it. This used to work thanks to the implicit type name extension, I believe.

复制错误21484 表示使用typename可用于Comeau和英特尔编译器。因为MSVC将所有名称视为依赖,所以该编译器不需要(但允许)构造。

Duplicate bug 21484 indicates that 'using typename' works on Comeau and Intel compilers. Because MSVC treats all names as dependent, the construct is unnecessary (but permitted) for that compiler.

a href =http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14258#c15>在2011年12月13日的GCC 4.7中修正!

Fixed in GCC 4.7 on Dec 13 2011!

这篇关于从依赖基类访问类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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