Why const for implicit conversion?

It doesn't really have much to do with the conversion being implicit Moreover, it doesn't really have much to do with conversions It is really about rvalues vs lvalues When you convert 99 to type X the result is an rvalue In C++ results of conversions are always rvalues (unless you convert to reference type). It is illegal in C++ to attach non-const references to rvalues For example, this code will not compile X& r = X(99); // ERROR because it attempts to attach a non-const reference to an rvalue. On the other hand, this code is fine const X& cr = X(99); // OK because it is perfectly OK to attach a const reference to an rvalue The same thing happens in your code as well.

The fact that it involves an implicit conversion is kinda beside the point. You can replace implicit conversion with an explicit one implicit_conversion_func(X(99)) and end up with the same situation: with const it compiles, without const it doesn't Again, the only role the conversion (explicit or implicit) plays here is that it helps us to produce an rvalue. In general, you can produce an rvalue in some other way and run into the same issue int &ir = 3 + 2; // ERROR const int &cir = 3 + 2; // OK.

It doesn't really have much to do with the conversion being implicit. Moreover, it doesn't really have much to do with conversions. It is really about rvalues vs. lvalues.

When you convert 99 to type X, the result is an rvalue. In C++ results of conversions are always rvalues (unless you convert to reference type). It is illegal in C++ to attach non-const references to rvalues.

For example, this code will not compile X& r = X(99); // ERROR because it attempts to attach a non-const reference to an rvalue. On the other hand, this code is fine const X& cr = X(99); // OK because it is perfectly OK to attach a const reference to an rvalue. The same thing happens in your code as well.

The fact that it involves an implicit conversion is kinda beside the point. You can replace implicit conversion with an explicit one implicit_conversion_func(X(99)); and end up with the same situation: with const it compiles, without const it doesn't. Again, the only role the conversion (explicit or implicit) plays here is that it helps us to produce an rvalue.In general, you can produce an rvalue in some other way and run into the same issue int &ir = 3 + 2; // ERROR const int &cir = 3 + 2; // OK.

Per section 5.2.2 paragraph 5, when an argument to a function is of const reference type, a temporary variable is automatically introduced if needed. In your example, the rvalue 99 has to be put into a temporary variable so that that variable can be passed by (const) reference to the constructor of X. Were the reference argument not const, the compiler would not be able to silently introduce a temporary, which would consequently prevent the automatic constructor call.

The semantics of creating a non-const temporary there would in any case be rather confusing -- what happens if the constructor clobbers the temporary, and what bizarre side effects could that end up having? (Think std::auto_ptr -- the pointer on the right hand side would silently be robbed of its referent! ).

AndreyT is totally correct. However it's worth pointing out in his response what an rvalue and an lvalue are. An "rvalue" is a value that can go on the right-hand-size of an assignment -- hence the "r".

An "lvalue" can go on the left-hand-size, hence the "l". More exactly, an lvalue has an address in memory, while an rvalue may not. Technically all lvalues are also rvalues, since anything that can go on the left-hand side can also go on the right -- otherwise you couldn't do things like this: int x = y = 3; Therefore, it's more exact to refer to lvalues and non-lvalues, since rvalue isn't specific.

en.wikipedia.org/wiki/Value_(computer_sc...).

1 That's a handy mnemonic for the rvalue/lvalue distinction, and it motivates the names, but it's not a proper definition. For example, arrays are lvalues, but cannot appear on the lhs of assignments. Variables defined with const are also lvalues, but also cannot appear on the lhs of assignments.

The result of dereferencing a pointer to a const-qualified type is, you guessed it... – Steve Jessop Oct 9 '10 at 16:50 Yes, the line about having an address in memory, that's the real definition. However, it's terribly difficult to remember in isolation, and only moderately useful. – tylerl Oct 10 '10 at 0:52 locator value – Roger Pate Oct 10 '10 at 2:17.

It doesn't really have much to do with the conversion being implicit. Moreover, it doesn't really have much to do with conversions. It is really about rvalues vs. lvalues.

When you convert 99 to type X, the result is an rvalue. In C++ results of conversions are always rvalues (unless you convert to reference type). It is illegal in C++ to attach non-const references to rvalues.

Because it attempts to attach a non-const reference to an rvalue. Because it is perfectly OK to attach a const reference to an rvalue. The same thing happens in your code as well.

The fact that it involves an implicit conversion is kinda beside the point. And end up with the same situation: with const it compiles, without const it doesn't. Again, the only role the conversion (explicit or implicit) plays here is that it helps us to produce an rvalue.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions