問題描述
根據本書 C++ Primer 部分,7.4.1 Type Names Are Special:
According to the book C++ Primer section, 7.4.1 Type Names Are Special:
通常,內部作用域可以從外部作用域重新定義名稱,即使該名稱已在內部作用域中使用.但是,在類中,如果成員使用外部作用域中的名稱并且該名稱是類型,則該類隨后可能不會重新定義該名稱.
Ordinarily, an inner scope can redefine a name from an outer scope even if that name has already been used in the inner scope. However, in a class, if a member uses a name from an outer scope and that name is a type, then the class may not subsequently redefine that name.
相應地,例如:
typedef double Money;
class Account {
public:
Money balance() { return bal; }
private:
typedef double Money;
Money bal;
};
int main() {
typedef double Money;
Money asset;
typedef double Money;
return 0;
}
當你編譯上面的例子時,它會報錯:
When you compile the example above, it will complain:
a.cc:6:24: error: declaration of ‘typedef double Account::Money’ [-fpermissive]
typedef double Money;
^
a.cc:1:16: error: changes meaning of ‘Money’ from ‘typedef double Money’ [-fpermissive]
typedef double Money;
那么為什么我們不能在類中重新定義類型名稱,而可以在內部作用域中?
So why can not we redefine type names in class, but can we in the inner scope?
我的編譯器版本是 g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
.
該部分還有一個注釋:
My compiler version is g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
.
And there is also a note in that section:
雖然重新定義類型名是錯誤的,但編譯器不需要診斷這個錯誤.一些編譯器會悄悄地接受這樣的代碼,即使程序出錯了.
Although it is an error to redefine a type name, compilers are not required to diagnose this error. Some compilers will quietly accept such code, even though the program is in error.
推薦答案
這不是類型獨有的.[basic.class.scope]/2:
This is not unique to types. [basic.class.scope]/2:
在類S
中使用的名稱N
應在其上下文以及在 S
的完整范圍內重新評估時.不違反此規則需要進行診斷.
A name
N
used in a classS
shall refer to the same declaration in its context and when re-evaluated in the completed scope ofS
. No diagnostic is required for a violation of this rule.
原因是類作用域中的名稱查找有點特殊.考慮:
The reason is that name lookup in class scope is a little special. Consider:
using Foo = int;
struct X {
Foo a; // ::Foo, i.e., int
void meow() {
Foo b = a; // X::Foo; error: no conversion from int to char*
}
using Foo = char*;
};
成員函數體中的名稱查找考慮所有類成員,無論是在成員函數之前還是之后聲明(否則,在類定義中定義的成員函數將無法使用在類中稍后聲明的數據成員).結果是您得到兩個具有不同含義的 Foo
,即使它們在詞法上都在類成員 Foo
的聲明之前.這很容易導致非常混亂和脆弱的代碼,因此標準禁止它.
Name lookup in member function bodies considers all class members, whether declared before or after the member function (otherwise, a member function defined in a class definition wouldn't be able to use a data member declared later in the class). The result is that you get two Foo
s with different meanings, even though they both lexically precede the class member Foo
's declaration. This can easily lead to extremely confusing and brittle code, and so the standard bans it.
這篇關于為什么不能在 C++ 中重新定義類中的類型名稱?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!