In Visual Studio 2012RC there are some non-standard extensions. For example this code compiles:
#include <string>
using namespace std;
void value(string& value)
{
value = "some";
}
int main()
{
value(string("nice"));
}
and get warning that it is non-standard extension. So, I want to understand, how it’s real and how code transforms (rvalue-reference or const reference with const_cast)?
A temporary object of class type is still an object. It lives somewhere in memory, which means that there’s nothing unusual in the compiler being able to attach a reference to it. At physical level whether it is a const reference or non-const reference makes no difference. In other words, in cases like that the language restriction is purely conceptual, artificial. The compiler simply ignores that restriction. There’s no need to “transform” anything here. The reference is simply attached directly to the object, wherever that object happens to reside.
Basically, for a class that provides the outside word with access to the value of its
thispointer (or with lvalue access to*this) the behavior can be immediately and easily simulatedThe above code is perfectly legal and it works around the aforementioned restriction. You can think of MSVC++ behavior as being equivalent to this.