What is the rationale behind the syntax chosen to declare template friends?

Viewed 219

Declaring template function friends involves some incredibly unintuitive syntax, even for C++! What is the rationale behind the choice of syntax for the extra <> needed? Wouldn't it make more sense to use the template keyword?

For those that don't know about this, here is an example of what you might try to do:

template <typename T>
class Foo
{
  int x;
  friend void bar(Foo<T>);
};

template <typename T>
void bar(Foo<T> f)
{
  std::cout << f.x;
}

If you try to call bar(Foo<T>()), you will get linker errors.

To solve this, you have to forward declare bar (and therefore Foo) and then stick an oddly placed <> in the friend declaration.

template <typename T> class Foo;
template <typename T> void bar(Foo<T>);

template <typename T>
class Foo
{
  int x;
  friend void bar<>(Foo<T>); // note the <> (!?)
};

template <typename T>
void bar(Foo<T> f)
{
    std::cout << f.x;
}

My question is, what is the rationale behind the <> syntax? Wouldn't it be more intuitive to use the template keyword or something along those lines?

EDIT: To clarify, I already know why the <> is required, what I want to know is why they chose to use <> to disambiguate it instead of some other more intuitive syntax.

2 Answers
Related