jeudi 4 juillet 2019

Class member function defined outside its namespace

The following code compiles perfectly with the latest MSVC, GCC and CLang available at godbolt online compiler explorer site. I wonder why:

namespace ns
{
    struct Test
    {
        void foo();
    };
}

using namespace ns;

// Alert! Member function defined outside its namespace!
void Test::foo()
{
}

int main()
{
    ns::Test   obj;
    obj.foo();
    return 0;
}

It's really unlikely that all three independent compilers have the same bug, right? So, is there a good reason behind them accepting such code?

Aucun commentaire:

Enregistrer un commentaire