不明确的注入类名不是错误 [英] Ambiguous injected class name is not an error
问题描述
我在C ++标准中读到的关于注入类名的内容与我将要介绍的示例程序的行为相矛盾。这是我读的:
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:
-
从3.4(第3段)
From 3.4 (paragraph 3)
为了名称隐藏和查找的目的,类(第9节)的inject-class-name也被认为是该类的成员
a。
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段)
From 9 (paragraph 2)
name被插入到它被声明的范围
紧接在看到class-name之后。类名也是
插入到类本身的范围内;这被称为
inject-class-name。为了访问检查,
inject-class-name被视为公共成员名。
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;
};
但是,我认为以下应该产生错误, p>
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>
};
由于向量
std :: vector< int>
和 std :: vector< char>
它应该由X继承,因此 X
中的名称向量
应该是不明确的。我缺少什么?
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?
我使用MSVC9.0
P.S. I am using MSVC9.0
推荐答案
我找到了!它在那里的标准!我是对的! 应该含糊不清。
I found it! It's right there in the standard! I was right! It should be ambiguous!
第14.6.1节
找到一个inject-class-name(10.2)的查找在某些情况下可能导致
模糊性(例如,如果发现它超过
一个基类)。如果所有找到的注入类名称
引用相同类模板的特殊化,并且如果名称
后面是模板参数列表,则引用引用
类模板本身,而不是它的专门化,而不是
模糊。 [示例:
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]
—end example]
底线: 这是另一个 Microsoft编译器BUG 。停用语言扩展功能也无济于事。
Bottom line: This is yet another Microsoft compiler BUG. Disabling language extensions doesn't help either.
这篇关于不明确的注入类名不是错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!