不明确的注入类名不是错误

发布于 2024-11-28 23:11:45 字数 904 浏览 1 评论 0原文

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

  • 来自 3.4(第 3 段)

    <块引用>

    类的注入类名(第 9 条)也被认为是 该类的成员,用于名称隐藏和查找。

  • 从 9(第 2 段)开始

    <块引用>

    类名被插入到声明它的范围中 看到类名后立即。类名也是 插入到类本身的范围内;这被称为 注入类名。出于访问检查的目的, Injected-class-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 被注入到 std::vector 和 < code>std::vector就像公共成员名称一样,那么它应该由 X 继承,因此 X 中的名称 vector 应该模棱两可。我错过了什么吗?

PS我使用的是MSVC9.0

What I read in the C++ standard about injected class names contradicts (as I see it) with the behavior of a sample program I will present shortly. Here's what I read:

  • From 3.4 (paragraph 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.

  • From 9 (paragraph 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.

From these I understand that the following is a well-formed translation unit and it compiles successfully.

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

However, I would suppose that the following should have produced an error, but it doesn't

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

Since the name vector is injected into both std::vector<int> and std::vector<char> as as if a public member name, then it should be inherited by X and therefore the name vector in X should be ambiguous. Am I missing something?

P.S. I am using MSVC9.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

用心笑 2024-12-05 23:11:46

我找到了!就在标准里!我是对的!它应该是不明确的!

第 14.6.1 条

查找注入类名 (10.2) 可能会导致
在某些情况下存在歧义(例如,如果发现超过
一个基类)。如果找到所有注入的类名
引用同一类模板的特化,并且如果名称
后面跟着一个 template-argument-list,引用指的是
类模板本身而不是其特化,并且不是
模糊的。 [示例:

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

—结束示例]

底线:这是另一个Microsoft 编译器错误。禁用语言扩展也没有帮助。

I found it! It's right there in the standard! I was right! It should be ambiguous!

Clause 14.6.1 Paragraph

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]

Bottom line: This is yet another Microsoft compiler BUG. Disabling language extensions doesn't help either.

贵在坚持 2024-12-05 23:11:46

不,你没有遗漏任何东西,而且你的编译器似乎有问题。你可以在这里看到 gcc 如何处理它: http://ideone.com/MI9gz

它的错误消息是:

prog.cpp:4:4: error: reference to 'vector' is ambiguous
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_vector.h:171:5: error: candidates are: class std::vector<char> std::vector<char>::vector
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_vector.h:171:5: error:                 class std::vector<int> std::vector<int>::vector

No, you are not missing anything, and your compiler seems to behave buggy. You can see how gcc handles it here: http://ideone.com/MI9gz

Its error message is:

prog.cpp:4:4: error: reference to 'vector' is ambiguous
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_vector.h:171:5: error: candidates are: class std::vector<char> std::vector<char>::vector
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_vector.h:171:5: error:                 class std::vector<int> std::vector<int>::vector
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文