Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must &= always be interpreted as an operator?

I was coding and accidentally left out a space between a constant reference and its default value. I was surprised to see that it came up as an error in Intellisense, so I compiled it, and sure enough, it doesn't work in GCC 4.3.4, 4.5.1, or 4.7.2, and doesn't work in Visual Studio 2012, either.

Here's an equivalent sample that demonstrates the error:

struct S {
    S(const int &= 5){}    
};

int main(){}

This yields the following error in GCC, and similar ones in MSVC:

error: expected ',' or '...' before '&=' token

I presume this is because &= is being treated as an operator, but I don't know exactly what to search for in the standard to find more information about this case. &= just comes up with operator-specfic information.

Being curious, I decided to swap it out for an rvalue reference:

S(int &&= 5){}

Strangely enough, this compiles fine on both GCC 4.7.2 and MSVC, which means that &= isn't always lexically paired as an operator.

Why does it work with an rvalue reference, but not an lvalue reference, and what does the standard have to say on the matter?

like image 283
chris Avatar asked Nov 29 '12 20:11

chris


People also ask

How do we use must?

Must is used to express obligation, give orders and give advice. It can only be used for present and future reference. When the past is involved, you use have to.

What is the full meaning of must?

Definition of must (Entry 1 of 4) auxiliary verb. 1a : be commanded or requested to you must stop. b : be urged to : ought by all means to you must read that book. 2 : be compelled by physical necessity to one must eat to live : be required by immediate or future need or purpose to we must hurry to catch the bus.

Is Must a smell?

Another meaning of the noun must is a stale, stuffy smell. Your damp basement and dry, hot attic might both smell of must. This meaning comes from the adjective musty, a variation on the older, now obsolete moisty.

What is the synonym of must?

See definition of must on Dictionary.com. nounnecessity, essential. verbought, should.


2 Answers

This is generally known as the "principle of longest match", or the "maximal munch". Because && is a valid token and &&= is not (there's no compound-assignment notation for &&), the longest token that &&= starts with is &&; after that's been removed, there's no opportunity for &= to be seen as a single token.

This principle is common to many languages, though there are often exceptions to it. For example, in C++11, >> will be analyzed as > followed by > in a context like std::vector<std::vector<int>>.

like image 193
ruakh Avatar answered Oct 11 '22 16:10

ruakh


The parser just works left to right, regardless of associativity, so in the first example, the first full token it finds is the &=. (At that moment, the parser doesn't check for larger constructs yet, so all it knows is that there's that token there.)

In the second example, the token it finds is &&. Because &&= is not a token!

like image 24
Mr Lister Avatar answered Oct 11 '22 17:10

Mr Lister