不明确的注入类名不是错误
我在 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::vectorX
中的名称 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了!就在标准里!我是对的!它应该是不明确的!
第 14.6.1 条
底线:这是另一个Microsoft 编译器错误。禁用语言扩展也没有帮助。
I found it! It's right there in the standard! I was right! It should be ambiguous!
Clause 14.6.1 Paragraph
Bottom line: This is yet another Microsoft compiler BUG. Disabling language extensions doesn't help either.
不,你没有遗漏任何东西,而且你的编译器似乎有问题。你可以在这里看到 gcc 如何处理它: http://ideone.com/MI9gz
它的错误消息是:
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: