c – 由多重继承隐藏的嵌套类

前端之家收集整理的这篇文章主要介绍了c – 由多重继承隐藏的嵌套类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这段代码有效C(11)吗?
struct Base {
    template <typename>
    struct nested;
};
struct Derived1 : Base { };
struct Derived2 : Base { };
struct Derived3 : Derived1,Derived2 { };

typedef Derived3::nested<int> xxx;

我知道的

上面的代码无法编译:

> Apple LLVM 5.0(clang-500.2.75)
> Clang 3.4

但它成功编译:

> gcc 4.9.0 20131110(实验性)
> gcc 4.8

另外,如果我将嵌套类型更改为非模板类​​型,即

struct Base {
    struct nested;
};
...
typedef Derived3::nested xxx;

然后它适用于上述编译器.

[编辑]
将嵌套模板结构更改为模板别名也不会改变任何内容;

template <typename> struct dependent { struct type; };
struct Base {
    template <typename T>
    using nested = typename dependent<T>::type;
};

与上述编译器产生相同的结果.
[结束编辑]

来自N3242§10.1[class.mi]

A class can be an indirect base class more than once and can be a direct and an indirect base class. There are limited things that can be done with such a class. The non-static data members and member functions of the direct base class cannot be referred to in the scope of the derived class. However,the static members,enumerations and types can be unambiguously referred to.

我认为这意味着代码应该是有效的,但我不确定.

解决方法

这是好的GCC是对/更有帮助(它坚持标准非常强烈)

它无法理解为什么定义会模糊不清,因为你所说的类型不是成员,如果它们的名字在C中是相同的,那么类型是相等的(名称是涉及类型的一些错位形式等)

附录:

如果在另一个基础中“嵌套”和“嵌套”不同,那就错了.它是一个结构,而不是typedef或使用(它们是作用域)

如果某些事情含糊不清,海湾合作委员会会发牢骚,如果你想让它变成婊子,即使它不是,也会尝试使用-santant.即使海湾合作委员会只是宽容,我也没有理由认为这应该被拒绝.

原文链接:https://www.f2er.com/c/118399.html

猜你在找的C&C++相关文章