VS2008 (+?) Compiler error with function templates and "use of namespace"

I found that this odd case of some code (below) does not compile in Visual Studio 2008 and produces "error C2872:" Ambiguity ": ambiguous character" on line 12.

Removing use namespace RequiredNamespacein the last line fixes the error, but I expect that putting using namespaceat the end of the file should not have any effect. It also relies on AnotherFunctionas a template function, so I expect the compiler to generate template functions in the wrong area or not reset the list of namespaces that will be used before.

The same code compiles under GCC.

Both compilers seem to generate code for TemplatedFunctionafter determining using namespace Namespace, at least as far as I can tell, by inputting errors and looking at how they are 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 the real case when it using namespaceis in an automatically generated file created by third-party code.

Is this a bug in the compiler?

+5
source share
2 answers

I believe that this is a mistake, according to clause 7.3.4 of paragraph 1 of the C ++ 03 standard:

The using directive states that names in the nomenclature namespace can be used in the area in which the using directive appears after the use directive.

, .

+2

, , , , , ( /Fa cl.exe).

, using, .asm . , , ( ??$TemplatedFunction@H@Ambiguity@@YAXH@Z PROC), , ( _main PROC). , ". ", , , .

NonTemplatedFunction(int a) , . , , , NonTemplatedFunction(int a), _main PROC.

? Visual Studio 2008 , . , "". gcc , , .

+5

All Articles