Reputation: 392
I am trying to implement a class that supports concatenation with different operators:
class MyClass {
public:
template<typename T>
MyClass &operator<<(const T& val ) {
//do something with val
return *this;
}
template<typename T>
MyClass &operator=(const T& val) {
//do something with val
return *this;
}
};
int main() {
MyClass a;
a << "hallo" = 3 << "huuh"; //compiler will complain about
}
Do i miss something here?
Thanks a lot for your help!
Upvotes: 1
Views: 54
Reputation: 21038
Due to operator precedence, the expression
a << "hallo" = 3 << "huuh";
is evaluated as
(a << "hallo") = (3 << "huuh");
and your compiler is complaining about the lack of a valid operator<<(int, const char[5])
.
You will need to use brackets to change the precedence:
(a << "hallo" = 3) << "huuh";
That said, it is extremely hard to understand what is going on here, operators should be used to make things clearer, not harder to read.
Upvotes: 4