No "redefinition of default parameter error" for class template member function?

Posted by STingRaySC on Stack Overflow See other posts from Stack Overflow or by STingRaySC
Published on 2010-03-19T18:52:32Z Indexed on 2010/03/19 19:21 UTC
Read the original article Hit count: 209

Filed under:
|
|
|

Why does the following give no compilation error?:

// T.h

template<class T> class X
{
public:
    void foo(int a = 42);
};

// Main.cpp

#include "T.h"
#include <iostream>
template<class T> void X<T>::foo(int a = 13)
{
    std::cout << a << std::endl;
}

int main()
{
    X<int> x;
    x.foo();   // prints 42
}

It seems as though the 13 is just silently ignored by the compiler. Why is this?
The cooky thing is that if the template declaration is in Main.cpp instead of a header file, I do indeed get the default parameter redefinition error.

Now I know the compiler will complain about this if it were just an ordinary (non-template) function.

What does the standard have to say about default parameters in class template member functions or function templates?

© Stack Overflow or respective owner

Related posts about c++

Related posts about visual-c++