hanDerPeder
hanDerPeder

Reputation: 397

Why does this template specialization fail when using a default parameter on the second invocation?

struct MyStruct
{
    MyStruct() {}
};
struct MySndStruct
{
    MySndStruct() {}
};

template <typename T> 
T Func(const T& t = T()) 
{ 
    T ret;
    return ret;
}

template <> 
MyStruct Func<MyStruct>(const MyStruct& ms)
{
    MyStruct ret;
    return ret;
}
int main()
{
    Func<MySndStruct>();
    Func<MySndStruct>(); // Works

    Func<MyStruct>();
    Func<MyStruct>(); // Fails
    Func<MyStruct>(MyStruct()); // Works fine
    return 0;
}

The second invocation of the MyStruct specialization for Func fails with the following error in Visual C++ 2010

error C2440: 'default argument' : cannot convert from 'MyStruct *' to 'MyStruct &

If I pass by value instead of by reference (MyStruct Func<MyStruct>(MyStruct ms)) the error becomes

fatal error C1001: An internal error has occurred in the compiler.

Is this a bug in Microsoft's c++ compiler or is default parameters not supported with template specializations?

If anyone can explain to me whats happening here, for instance, why does the compiler assume a pointer is passed in when it's IMO clearly not? And why does the first invocation work? I assume it has something to do with instantiating the template, but I'd like to know more precisely.

EDIT: Updated parameters to be const to highlight that this does not make a difference on Visual C++ 2010.

Upvotes: 0

Views: 170

Answers (1)

Walter
Walter

Reputation: 45434

replace T Func(T& t = T()) with T Func(const T& = T()) and it shall compile fine.

Upvotes: 1

Related Questions