7

C++11 標準から、§7.3.3[namespace.udecl]/1:

using 宣言は、using 宣言が現れる宣言領域に名前を導入します。

使用宣言:

using typenameopt ネストされた名前指定子 非修飾 ID ;
using :: 非修飾 ID ;

using宣言で指定されたメンバー名は、using宣言が現れる宣言領域で宣言されます。

using宣言が発生する宣言領域で宣言されている名前はどういう意味ですか?

これは、using 宣言が発生する宣言領域にその名前を導入することと同じ意味ですか?

また、名前を宣言することと、名前が示すエンティティを宣言することには違いがありますか?

例:

namespace N { static int i = 1; } /* Declares an entity denoted by 
    the name i in the declarative region of the namespace N. 
    Introduces the name into the declarative region of the namespace N.
    Declares the name i in the declarative region of the namespace N? */
using N::i; /* Declares the name i in the declarative region of the
    global namespace. Also introduces that name into the declarative
    region of the global namespace? Also declares the entity that the
    name i denotes? */ 
4

2 に答える 2

6

第一原理から、エンティティは、[基本] から

値、オブジェクト、参照、関数、列挙子、型、クラス メンバー、ビット フィールド、テンプレート、テンプレートの特殊化、名前空間、パラメーター パック、またはthis. [...] エンティティを表すすべての名前は、宣言によって導入されます。

宣言は物事を宣言します。宣言されるとは、[basic.scope.declarative] からの宣言によって導入されたことを意味します。

すべての名前は、宣言領域と呼ばれるプログラム テキストの一部で導入されます。宣言領域は、その名前が有効なプログラムの最大の部分です。つまり、その名前は、同じエンティティを参照する非修飾名として使用できます。 .

宣言によって宣言された名前は、宣言が発生するスコープに導入さ friendます。 4) この一般的な動作を変更します。

using-declarationsについて話しているのであって、 using-directivesについて話しているのではないので、これらの例外はここでは関係ありません。グローバル名前空間を回避するために、例を多少変更させてください。

namespace N {        //  + declarative region #1
                     //  |
    static int i;    //  | introduces a name into this region
                     //  | this declaration introduces an entity
}                    //  +

まず、N::iは名前空間で宣言Nされ、 のスコープに導入されるエンティティですN。それでは、using-declaration を追加しましょう:

namespace B {        //  + declarative region #2
                     //  |
    using N::i;      //  | declaration introduces a name i
                     //  | but this is not an entity
}                    //  +

[namespace.udecl] から、次のようになります。

using 宣言がコンストラクターを指定する場合(3.4.3.1)、using 宣言が表示されるクラスで一連のコンストラクターを暗黙的に宣言します (12.9)。それ以外の場合、 using-declarationで指定された名前は、別の名前空間またはクラスの一連の宣言の 同義語です。

using-declarationusing N::iコンストラクターに名前を付けないため、名前iを新しいエンティティーにするのではなく、 のシノニムにしN::iます。

したがって、基本的に、両方iの は、それぞれの名前空間で導入および宣言された名前です。ではNi静的リンケージを持つエンティティを宣言しますが、Bではi、新しいエンティティではなく、そのエンティティのシノニムを宣言します。

于 2015-07-29T20:56:24.493 に答える