c++ - Error calling user-defined operator+ on temporary object when there are extra brackets -


if have user-defined operator+() in:

    class     {     public:         operator+(a)         {             return a();         }     }; 

then following works expected:

    a = a() + a(); 

but g++-4.7 gives error message on following:

    a = (a()) + a(); 

the error message in particular error: no match ‘operator+’ in ‘+a()’.
looks (a()) being ignored in expression.

my question is: a = (a()) + a(); supposed compile , if not, why not?

note: happened me when did #define x (identity()) , tried doing x + x.

it's cast syntax.

the reason casting , unary addition, subtraction , multiplication (the dereference operator) have higher precedence binary counterparts. since white spaces here don't matter can read as:

    a = (a()) +a(); 

the cast , unary+ have higher precedence binary operator+ expression takes former meaning.

you might wondering (as did) how can cast when thing inside not type. enter the vexing parse!, means trying cast object of type +a() function taking 0 arguments , returning object of type a.

for record, syntax:

    a = ((a())) + a(); 

gives want since double brackets can't cast , we're parsing binary operator+ expression.

this explains why problem doesn't occur division operator instead of addition, doesn't have unary counterpart.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -