有谁知道为什么使用声明似乎不适用于从依赖基类导入类型名称?它们适用于成员变量和函数,但至少在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
};
我有一个基类,有一组allocator样式的typedef,我想在几个继承级别继承 . 到目前为止,我发现的最佳解决方案是上面的版本3,但我很好奇为什么版本4似乎不起作用 . GCC接受使用声明,但似乎忽略它 .
我检查了C标准,C Prog . 郎 . 第3版 . [Stroustrup]和C模板[Vandevoorde,Josuttis],但似乎都没有解决使用声明是否可以应用于依赖基类类型 .
如果它有助于在GCC邮件列表上看到另一个例子here is the same question being asked,但没有真正回答 . 提问者表示他已经在其他地方见过'using typename',但GCC没有其他符合标准的编译器可供测试 .
2 回答
正如理查德科登指出的那样,2003年标准获得批准后,这个问题在_2971542中得到了解决:How do the keywords typename/template interact with using-declarations?
该文本似乎没有出现在2003年10月15日的第二版标准中 .
海湾合作委员会尚未执行该决议,如_2971544所述:
重复bug 21484表示'using typename'适用于Comeau和Intel编译器 . 因为MSVC将所有名称视为依赖,所以该编译器不需要(但允许)该构造 .
Fixed in GCC 4.7 on Dec 13 2011!
在为Base :: value_type声明typedef之前,您没有在Base的模板中包含访问说明符(public / protected / private) . 因此,它默认为private,并且在从Base派生的类中不可访问 .