Sujet : Re: Namespace vs. class ambiguity: three compilers - three different outcomes
De : Bonita.Montero (at) *nospam* gmail.com (Bonita Montero)
Groupes : comp.lang.c++Date : 07. Sep 2024, 20:42:26
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vbi6qd$1fpiu$1@raubtier-asyl.eternal-september.org>
References : 1
User-Agent : Mozilla Thunderbird
Am 07.09.2024 um 20:24 schrieb Andrey Tarasevich:
Hello
It is illegal to just flat-out declare a class and a namespace with identical names in the same scope
namespace N {}
class N {}; // <- Not allowed
However, one can try to circumvent the direct restriction by means of using-directive or using-declaration
#include <iostream>
namespace N
{
void foo() { std::cout << "namespace" << std::endl; }
}
namespace X
{
struct N
{
static void foo() { std::cout << "class" << std::endl; }
};
}
using X::N;
// or
// using namespace X;
int main()
{
N::foo();
}
GCC is perfectly happy with either version of this code (both using- declaration and using-directive versions are OK). It simply resolves the call to the "namespace" version of `foo()`.
Clang issues an error: it complains about the call being ambiguous. I.e. the error is issued at the point of the call.
MSVC++ issues an error for `using X::N;` at the point of using- declaration: it basically says that `N` already exists in this scope. But if we switch to `using namespace X;` version, MSVC++ will exhibit Clang-like behavior: complain about ambiguity at the point of the call.
So, who is right here?
Does it really matter or is it just sufficient to prevent such code ?