views:

250

answers:

1

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 class template definition 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?

+3  A: 

8.3.6 §6 The default arguments in a member function definition that appears outside of the class definition are added to the set of default arguments provided by the member function declaration in the class definition.
[Example:

class C {
    void f(int i = 3);
    void g(int i, int j = 99);
};
void C::f(int i = 3) // error: default argument already
{ }                  // specified in class scope
void C::g(int i = 88, int j) // in this translation unit,
{ }                          // C::g can be called with no argument

--end example]

According to the standard, it should give you an error.

Bertrand Marron
@tusbar you are quoting only part of that paragraph. It says at the start of it "Except for member functions of class templates, ...".
Johannes Schaub - litb
Interesting: C++03 differs from C++98. What @tusbar quoted is the C++98 text, while C++03 added the exception for class templates and added to the paragraph "Default arguments for a member function of a class template shall be specified on the initial declaration of the member function within the class template." - i suspect that this is meant to forbid default arguments in out-of-class definitions in case of members of class templates by saying that any default arguments shall be specified in-class [instead of outside of the class] however to me it seems unclear (does it forbid duplicates?).
Johannes Schaub - litb
I looked into http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#217 and it seems like the intention is to forbid any out-of-class default arugments for template members. Also, i think now since the out-of-class default argument is regarded distinct from the one in-class, even if lexically identical, it violates the "shall be specified on the initial declaration" constraint. So this makes sense if you get a compile time error. The "issue 3.13" John Spicer refers to can be read in this document: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1995/N0607.pdf
Johannes Schaub - litb