2

Is it allowed to provide a default to a template argument in a friend declaration?

class A {
    int value;
public:
    template<class T = int> friend void foo();
};

Visual Studio 2015 seems to allow it. gcc refuses it. I couldn't find anything on it on the cppreference page.

Cubbi
  • 46,567
  • 13
  • 103
  • 169
xtofl
  • 40,723
  • 12
  • 105
  • 192

2 Answers2

3

As of C++11, the rule is, specified in 14.1[temp.param]/9

If a friend function template declaration specifies a default template-argument, that declaration shall be a definition and shall be the only declaration of the function template in the translation unit.

Until C++11, of course, 14.1/9 said "A default template-argument shall not be specified in a friend template declaration."

(the above is copied pretty much verbatim, by cppreference at Default template parameters, now also mentioned at Template friends)

So, to make your program valid C++, define your friend template inside the class, don't just declare.

Cubbi
  • 46,567
  • 13
  • 103
  • 169
2

If you really want to keep your function foo() global, you can try this:

class A
{
    int value;
public:
    template<class T> friend void foo();
};

template<class T = int> void foo()
{
    //you can use private member of A
    A foo;
    auto value = foo.value;
}
janosik
  • 21
  • 2