warsac
warsac

Reputation: 352

Template class Y in namespace X with class X can't compile in VS2010

When I have a class with the same name as the namespace it is contained in (A::A) and then define a templated class in the same namespace the compilation fails if and only if I define the functions of the templated class outside of the namespace using the scope resolution operator (A::B<type>::...). This is in Visual studio 2010. When I compile the code at http://ideone.com/ it works in both cases below.

Can anyone explain why this might happen?

namespace A
{
    template<typename type>
    class B
    {
    public:
        B();
    };

    class A
    {
    };  
}

/* 1. Doing this works
namespace A
{
    template<typename type>
    B<type>::B()
    {
    }
}
*/

/* 2. error C2039: 'B' : is not a member of 'A::A'
template<typename type>
A::B<type>::B()
{
}
*/

int main()
{
    A::B<int> test;
}

Upvotes: 3

Views: 120

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 311038

It is a compiler bug. If you will define the constructor the following way

template<typename type>
::A::B<type>::B()
{
}

when MS VC++ will compile the code without errors.

Upvotes: 1

Constructor
Constructor

Reputation: 7473

It looks like a bug. You can make Visual C++ to compile the code 2 in the following manner:

template<typename type>
::A::B<type>::B()
{
}

Upvotes: 0

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

Class and namespace names may clash in ! Don't use classnames named the same as their containing namespace.
I have no standard cite about how this should behave at hand actually, but experienced this a number of times. It seems to be compiler implementation dependent behavior.

Upvotes: 0

Related Questions