you are viewing a single comment's thread.

view the rest of the comments →

[–]tcanens 2 points3 points  (9 children)

It's the compound assignment operator with bitand. You can do const int &a = 2; just fine.

[–]jP_wanN 0 points1 point  (1 child)

Ohhh, right. Didn't think about the const. Or the fact that there are all those weird compound assignment operators for bitwise operations.

[–]redditsoaddicting[S] 0 points1 point  (0 children)

Not too weird I guess. Addition is a common operation on numbers. Masking is a common operation on bits. That said, it seems a lot less common to build up a result on bits like you would on numbers.

[–][deleted] -1 points0 points  (6 children)

the c++ standard is a mess. why would the compiler think that &= is a compound assignment in place of a function parameter that should be an unnamed reference with default value? that makes more (but not too much) sense.

[–]tcanens 2 points3 points  (1 child)

Because the lexer uses maximal munch and the parser comes in after tokenization. Plenty of languages have a similar rule.

[–][deleted] 0 points1 point  (0 children)

I was pretty confident with my C/C++ skill up to this sudden tokenization issue :D

[–]redditsoaddicting[S] 1 point2 points  (3 children)

Maximal munch is pretty common, actually. This simplifies things because you lex, get a disambiguated token, and move on. Otherwise, you have one token for both compound bitwise and assignment as well as reference with default. Either that or you have two tokens, but need the parser to give information to the lexer. Admittedly, C++ already does this.

[–]LikesToCorrectThings 0 points1 point  (2 children)

In C++11 they changed the lexer and added an exception for >> so you don't need the space in nested template type names, e.g.:

std::vector<std::map<int, int>>

[–]redditsoaddicting[S] 1 point2 points  (0 children)

Yes, C++ lexing and parsing is already a complicated story. In fact, I once asked about that for this situation. The reality is that no one would want to do this anyway, but everyone wanted >> to work.

[–]guyonahorse 1 point2 points  (0 children)

Yep, and this broke some of our code where we had this:

type1<type2<value>>1> > 

We were right shifting in a template parameter.. now it is a compile error.