gpt4 book ai didi

c++ - 从依赖基类访问类型

转载 作者:可可西里 更新时间:2023-11-01 15:22:14 25 4
gpt4 key购买 nike

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

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
};

我有一个带有一组分配器样式类型定义的基类,我想在多个继承级别中继承这些类型定义。到目前为止我找到的最好的解决方案是上面的版本 3,但我很好奇为什么版本 4 似乎不起作用。 GCC 接受 using 声明,但似乎忽略了它。

我已经检查了 C++ 标准、C++ Prog。郎。第三版。 [Stroustrup] 和 C++ 模板 [Vandevoorde, Josuttis],但似乎都没有解决 using 声明是否可以应用于依赖基类类型的问题。

如果看另一个例子有帮助,here is the same question being asked ,但在 GCC 邮件列表上没有真正回答。提问者表示他在其他地方看到过“使用类型名”,但 GCC 似乎不支持它。我没有其他符合标准的编译器可用于测试它。

最佳答案

正如 Richard Corden 指出的那样,此问题已在 C++ Standard Core Language Defect Reports 中得到解决2003年标准批准后:How do the keywords typename/template interact with using-declarations?

Proposed resolution (April 2003, revised October 2003):

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

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 日的第二版标准中。

GCC 尚未实现此决议,如 bug 14258 中所述:

------- 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.

重复 bug 21484表示 'using typename' 适用于 Comeau 和 Intel 编译器。因为 MSVC 将所有名称视为相关名称,所以该构造对于该编译器来说是不必要的(但允许)。


Fixed 2011 年 12 月 13 日在 GCC 4.7 中!

关于c++ - 从依赖基类访问类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1071119/

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