gpt4 book ai didi

c++ - 模棱两可的注入(inject)类名不是错误

转载 作者:IT老高 更新时间:2023-10-28 21:57:28 25 4
gpt4 key购买 nike

我在 C++ 标准中读到的关于注入(inject)类名的内容与我将很快介绍的示例程序的行为相矛盾(如我所见)。这是我读到的:

  • 从 3.4(第 3 段)开始

    The injected-class-name of a class (clause 9) is also considered to be a member of that class for the purposes of name hiding and lookup.

  • 从 9(第 2 段)开始

    A class-name is inserted into the scope in which it is declared immediately after the class-name is seen. The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name. For purposes of access checking, the injected-class-name is treated as if it were a public member name.

据我了解,以下是格式正确的翻译单元,并且编译成功。

#include <vector>
class X: std::vector<int>
{
vector mem;
};

但是,我认为以下应该产生错误,但它没有

#include <vector>
class X: std::vector<int>, std::vector<char>
{
vector mem; //compiles OK... mem is apparently std::vector<int>
};

起名vector注入(inject)两个 std::vector<int>std::vector<char>就像一个公共(public)成员名称一样,那么它应该被 X 继承,因此名称 vectorX应该是模棱两可的。我错过了什么吗?

附:我正在使用 MSVC9.0

最佳答案

我找到了!它就在标准中!我是对的!它应该模棱两可!

第 14.6.1 条

A lookup that finds an injected-class-name (10.2) can result in an ambiguity in certain cases (for example, if it is found in more than one base class). If all of the injected-class-names that are found refer to specializations of the same class template, and if the name is followed by a template-argument-list, the reference refers to the class template itself and not a specialization thereof, and is not ambiguous. [Example:

template <class T> struct Base { };
template <class T> struct Derived: Base<int>, Base<char>
{
typename Derived::Base b; // error: ambiguous typename
Derived::Base<double> d; // OK
};

—end example]

底线:这是另一个Microsoft 编译器BUG。禁用语言扩展也无济于事。

关于c++ - 模棱两可的注入(inject)类名不是错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7025054/

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