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

前端 未结 2 513
被撕碎了的回忆
被撕碎了的回忆 2021-02-14 23:37

Declaring template function friends involves some incredibly unintuitive syntax, even for C++! What is the rationale behind the choice of syntax for the extra <>

2条回答
  •  执念已碎
    2021-02-15 00:07

    Quoting this page:

    The non-template function is called because a non-template function takes precedence in overload resolution.

    So my guess the friend void bar<>(Foo); is intuitive because you want to make sure it's the template function you are calling and not an overload for Foo. There would otherwise no way for the compiler to notice if the templated or the non-templated function is the class'es friend.

    Why the <> syntax is used and not the template syntax is not clear to me.

提交回复
热议问题