Sollen Funktionen in einem Namespace nicht nur über den Namespace-Scoping oder die using-Direktive erreichbar sein?Warum sind einige Funktionen in einem Namespace ohne das Namespace-Scoping-Präfix verfügbar?
Ich habe ein Problem, wo bestimmte Funktionen, die innerhalb eines Namespace definiert sind, außerhalb des Namespace zugänglich sind. Ich glaube, dass es einen Compiler-Fehler geben sollte, aber ich bekomme keinen über die drei verschiedenen Compiler, die ich versucht habe (VS.NET 2003, VS2010 und GCC 4). Hier
ist der Code:
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!
}
Keine der Funktionen sollten ohne Verwendung des N :: Präfix zugänglich sein, aber C, D und E sind aus unbekannten Gründen. Ich dachte zuerst, es wäre ein Compiler-Bug, aber weil ich das über mehrere Compiler sehe, frage ich mich, was passiert.
Ich dachte, das war ein "Old-Compiler-Bug", aber das kompiliert auch auf gcc 4.6. – mfontanini
Meine beste Vermutung ist die Tatsache, dass Sie diese Zeilen haben: 'mit N :: some_type;' und 'mit N :: some_struct;' und die Rückgabetypen von C, D und E sind diese Typen. – gcochard
Greg, du kannst den using entfernen und es passiert immer noch. Auch die Rückgabetypen aller Funktionen sind ungültig. – syplex