I have a function template that I'm declaring as a friend of my class. The thing is, I want to keep the declaration and definition separate because I don't want to clutter the body of X
, but I'm running into problems when using the default argument of T()
:
struct X
{
template <class T>
friend void f(T t);
};
template <class T>
void f(T t = T()) {}
// error: default arguments cannot be added to a function template that has
// already been declared
// void f(T t = T()) {}
// ^ ~~~
// main.cpp:9:17: note: previous template declaration is here
// friend void f(T t);
// ^
If I switch it around to use default arguments in the declaration instead, then the compiler outputs a message saying I have to define the function there, but I don't want to do that.
How do I give a default argument to a friend function template while keeping the definition and declaration separate?
For function templates you may only write the default arguments in the first declaration in any given scope; and, indeed, there is another rule that prohibits writing the default arguments in the friend declaration:
[C++11: 8.3.6/4]:
For non-template functions, default arguments can be added in later declarations of a function in the same scope. [..] If a friend declaration specifies a default argument expression, that declaration shall be a definition and shall be the only declaration of the function or function template in the translation unit.
However, there's nothing stopping you from adding yet another declaration, this time not a friend declaration, before everything else, and put your default arguments on that.
So, in order:
I think if you declare the template function before friending it that would work, but I can't figure out what your main.cpp has to test. Did you try this (EDIT: this seems to work on coliru but I can't test your real-world scenario).
template <class T>
void f(T t = T());
struct X
{
template <class T>
friend void f(T t);
};
template <class T>
void f(T t) {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With