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?
S(const int & x= 5){}
would work. I think you already know that though. Good question. Waiting forward to an answer. – Indigoid&
in&=
is a bitwise operator, not a reference qualifier. – Remission