Halt
Halt

Reputation: 2994

Two default parameters in template, what is wrong here?

The code below shows 2 Foo templates each with 2 default parameters, Foo1 has a separate prototype and Foo2 doesn't, they are otherwise the same.

Why will the first call into Foo1 cause the compiler (VS2010 Native C++) to produce an error while the other 3 work?

#include <limits>

// not needed but to prevent answers in this direction...
#undef max
#undef min

template< typename T >
void Foo1( T v1 = std::numeric_limits< T >::min(), T v2 = std::numeric_limits< T >::max() );

template< typename T >
inline
void Foo1( T v1, T v2 )
{
    // ...
}

template< typename T >
inline
void Foo2( T v1 = std::numeric_limits< T >::min(), T v2 = std::numeric_limits< T >::max() )
{
    // ...
}

int main()
{
    Foo1<int>(0);  /* Will cause  error C2589: '::' : illegal token on right side of '::' */
    Foo1<int>(0, 10);  
    Foo2<int>(0);
    Foo2<int>(0, 10);
}

Upvotes: 5

Views: 136

Answers (1)

dirkgently
dirkgently

Reputation: 111130

This is a compiler bug as reported here. The workaround seems to be:

Thank you for submitting this feedback. While we recognize that this is a valid compiler bug, it is below our triage bar at this point in the product cycle. The workaround is to define the template function where you have declared it. If you are concerned about the perf impact of recompiling the template function for each translation unit, using PCH files should eliminate this overhead.

Thanks, Mark Roberts Visual C++ Team

Upvotes: 3

Related Questions