名前空間スコープを使用するか、またはusingディレクティブを使用するだけで、名前空間内の関数にアクセスできないようにする必要がありますか?名前空間スコープ接頭辞なしで名前空間内の一部の機能にアクセスできないのはなぜですか?
名前空間の内部で定義された特定の関数がその名前空間の外部からアクセスできるという問題が発生しています。私はコンパイラのエラーがあるはずだと信じていますが、私が試した3つのコンパイラ(VS.NET 2003、VS2010、GCC 4)を渡すことはできません。機能の
namespace N{
typedef struct _some_type *some_type;
struct some_struct { int x; };
void A(void);
void B(int);
void C(some_type*);
void D(some_type);
void E(struct some_struct);
}
using N::some_type;
using N::some_struct;
void TestFunction()
{
some_type foo;
some_struct s;
N::A(); //should compile (and does on VS2003, VS2010, and GCC 4.1.2)
::A(); //shouldn't compile (and doesn't on VS2003, VS2010, and GCC 4.1.2)
A(); //shouldn't compile (and doesn't on VS2003, VS2010, and GCC 4.1.2)
N::B(0); //should compile (and does on VS2003, VS2010, and GCC 4.1.2)
::B(0); //shouldn't compile (and doesn't on VS2003, VS2010, and GCC 4.1.2)
B(0); //shouldn't compile (and doesn't on VS2003, VS2010, and GCC 4.1.2)
N::C(&foo); //should compile (and does on VS2003, VS2010, and GCC 4.1.2)
::C(&foo); //shouldn't compile (and doesn't on VS2003, VS2010, and GCC 4.1.2)
C(&foo); //shouldn't compile (but does on VS2003, VS2010, and GCC 4.1.2) -- problem!
N::D(foo); //should compile (and does on VS2003, VS2010, and GCC 4.1.2)
::D(foo); //shouldn't compile (and doesn't on VS2003, VS2010, and GCC 4.1.2)
D(foo); //shouldn't compile (but does on VS2003, VS2010, and GCC 4.1.2) -- problem!
N::E(s); //should compile (and does on VS2003, VS2010, and GCC 4.1.2)
::E(s); //shouldn't compile (and doesn't on VS2003, VS2010, and GCC 4.1.2)
E(s); //shouldn't compile (but does on VS2003, VS2010, and GCC 4.1.2) -- problem!
}
なしN ::接頭辞を使用せずにアクセスできるべきではありませんが、C、D、およびEは、いくつかの未知の理由である:ここでは
はコードです。私は当初、それがコンパイラのバグだと思っていましたが、これを複数のコンパイラで見ているので、何が起こっているのか疑問に思っています。
これは「古いコンパイラのバグ」だと思っていましたが、gcc 4.6でもコンパイルされていました。 – mfontanini
「N :: some_type;」と「N :: some_struct;」を使用し、C、D、Eの戻り値の型はこれらの型を持っていることを推測してください。 – gcochard
グレッグ、あなたは使用して削除することができ、それでも起こります。また、すべての関数の戻り値の型は無効です。 – syplex