Problem
Call an overloaded
operator=
on a temporary instance of a class.
Given a class
A
:
struct A {
A(int arg) { }
A& operator=(int num) { return *this; }
};
The following code will not compile with gcc-4.6.1 due to a "conflicting declaration" error:
int a;
A(a) = 4; // error: conflicting declaration
The compiler thinks that with
A(a)
we are trying to declare an instance of
A
named
a
.
Workaround
Wrapping the instantiation with round brackets will fix the problem.
int a;
(A(a)) = 4;
Strangely, when the operator is not the assignment one, the code compiles.
struct A {
A(int arg) { }
A& operator+=(int num) { return *this; }
};
...
int a;
A(a) += 4;
No comments:
Post a Comment