Consider such code:
template<typename S>
class C;
template<typename S>
C<S> operator-(C<S> lhs, C<S> rhs);
template<typename S>
class C
{
public:
C operator-() { return *this; }
friend C operator-<S>(C lhs, C rhs);//error on this line
};
template<typename S>
C<S> operator-(C<S> lhs, C<S> rhs) { return C<S>(); }
int main(int argc, char** argv)
{
C<int> a,b;
a-b;
return 0;
}
This gives me 6 errors in MSVC. But if I move the definition of C operator-()
after the friend declaration, it compiles. If I change the class to an untemplated class, it compiles. And this seems to compiles in g++ too. (I have no g++ installed, based on https://wandbox.org/)
So what's wrong here?
(C lhs, C rhs);` I get 9 errors. btw, "not needed" means it's no harmful to include them, am I right?– user Sep 12 '19 at 17:21