Resolution area for creating a template

I have the following set of classes (minimal replication of my real situation):

namespace Parent { class A {}; namespace Nested { class A {}; } template <typename T> class B { A myA; }; } 

I would expect the member Parent::B::myA to be uniquely resolved as a type of Parent::A However, elsewhere in my project, I have the following:

 namespace Parent { using namespace Nested; void foo() { B<int> myB; } } 

which does not compile in MSVC 2003:

 error C2872: 'A' : ambiguous symbol could be 'Test.cpp(5) : Parent::A' or 'Test.cpp(9) : Parent::Nested::A' Test.cpp(26) : see reference to class template instantiation 'Parent::B<T>' being compiled with [ T=int ] 

The code will be compiled if I am explicit in my B::myA , i.e. Parent::A myA; . However, the code compiles since it is under gcc-4.3.4 . Is this just a bug with MSVC 2003, or do I really need to worry about the area in which my templates can be created?

+6
source share
1 answer

This is a long bug in all versions of MSVC.

The problem is due to the incorrect implementation of name lookups in templates in MSVC.

Basically, MSVC will wait until the instantiation point does a name lookup, while the standard explicitly states that the correct behavior:

  • search for a name immediately (at the point of declaration) for non-dependent characters
  • search for a name when creating an instance for dependent characters

This allows MSVC to be weak regarding the use of typename or template , as it can completely deduce the nature of characters (to differentiate regular variables from functions or types), however this is a nightmare when you are aiming for compatibility with other compilers.

Release MSVC if you can. If you can’t ... good luck.

+6
source

Source: https://habr.com/ru/post/902106/


All Articles