I have the following MWE in C++20 with clang++ -std=c++2a, in which I defined in-class unary - operator and friend-ed binary - operator:
template<typename T>
class vec;
template<typename T>
vec<T> operator-(const vec<T>&, const vec<T>&);
template<typename T>
class vec {
public:
vec() {}
vec operator-() const { return vec(); }
friend vec operator-<>(const vec&, const vec&);
};
template<typename T>
vec<T> operator-(const vec<T>& lhs, const vec<T>& rhs) { return vec<T>(); }
int main()
{
vec<int> v;
return 0;
}
However, this results in the following error in C++17:
main.cpp:12:16: error: friends can only be classes or functions
friend vec operator-<>(const vec&, const vec&);
^
main.cpp:12:25: error: expected ';' at end of declaration list
friend vec operator-<>(const vec&, const vec&);
^
;
with Apple clang version 11.0.3 (clang-1103.0.32.59).
The error disappears when I remove the in-class unary operator, or when I use C++20 via -std=c++2a.
What is causing this issue in C++17, and how does C++20 resolves this issue? Any help would be greatly appreciated!