Domanda

The following bit of code compiles in GCC 4.5.3 but does not compile in VS 2008 and 2010. Is this due to a VS compiler bug or does the standard forbid giving default function template argument values?

#include <cstdlib>

struct Bar
{
    enum Group{ A , B , C };
};

struct Foo
{
    template<typename T>
    static void getSome( typename T::Group = T::A );
};

template<typename T>
void Foo::getSome( typename T::Group )
{
};

int main()
{
    Foo::getSome<Bar>();            // Does not compile in VS 2008 & 2010 (compiles in gcc 4.5.3)
    Foo::getSome<Bar>( Bar::C );    // Compiles in VS 2008 and gcc 4.5.3
    return EXIT_SUCCESS;
}

Error message

prog.cpp(11) : error C2589: '::' : illegal token on right side of '::'
prog.cpp(11) : error C2059: syntax error : '::'
È stato utile?

Soluzione

It is a MSVC bug.

The bug is in the handling of template functions with default parameters, as you probably guessed.

Their workaround is to supply all function parameters. (yuck)

Acknowledged here.

Altri suggerimenti

I think g++ is adhering to the standard when it compiles your snippet.

The following extract should be the reference to the right part of the standard (section 14.1.9):

A default template-argument is a template-argument (14.3) specified after = in a template-parameter. A default template-argument may be specified for any kind of template-parameter (type, non-type, template) that is not a template parameter pack (14.5.3). A default template-argument may be specified in a template declaration. A default template-argument shall not be specified in the template-parameter-lists of the definition of a member of a class template that appears outside of the member’s class. A default template-argument shall not be specified in a friend class template declaration. If a friend function template declaration specifies a default template-argument, that declaration shall be a definition and shall be the only declaration of the function template in the translation unit.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top