c++templatesnamespaces

VS2008(+?) compiler bug with templated functions and 'using namespace'


I've found this odd case of some code (below) doesn't compile under Visual Studio 2008 and produces an "error C2872: 'Ambiguity' : ambiguous symbol" on line 12.

Removing the using namespace RequiredNamespace on the last line fixes the error, but I'd expect that putting using namespace at the end of a file should have no effect. It also relies on AnotherFunction being a template function, so I expect the compiler is generating the templated functions in the wrong scope, or isn't resetting the list of namespaces being used before doing so.

The same code compiles under GCC.

Both compilers seem to generate the code for TemplatedFunction after the using namespace Namespace definition, at least as far as I can tell by introducing errors and looking at the order they're output.

namespace Ambiguity
{
    class cSomeClass
    {
    };

    template<class T>
    void TemplatedFunction(T a)
    {
        // this is where the error occurs, the compiler thinks Ambiguity
        // might refer to the class in RequiredNamespace below
        Ambiguity::cSomeClass(); 
    }
}

namespace RequiredNamespace 
{
    // without a namespace around this class, the Ambiguity class 
    // and namespace collide
    class Ambiguity
    {
    };
}

int main()
{
    // to force the templated function to be generated
    Ambiguity::TemplatedFunction(4); 
}

// removing this removes the error, but it shouldn't really do anything
using namespace RequiredNamespace; 

Obviously this is a manufactured example, but the original is extracted from a real case where the using namespace is in a auto-generated file produced by 3rd party code.

Is this a bug in the compiler?


Solution

  • I believe it's a bug, per 7.3.4 para 1 of the C++03 standard:

    A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive.

    So your end-of-file using declaration should have no effect.