After extensive reading of ISO/IEC 14882, Programming language – C++ I'm still unsure why const
is needed for implicit conversion to a user-defined type with a single argument constructor like the following
#include <iostream>
class X {
public:
X( int value ) {
printf("constructor initialized with %i",value);
}
}
void implicit_conversion_func( const X& value ) {
//produces "constructor initialized with 99"
}
int main (int argc, char * const argv[]) {
implicit_conversion_func(99);
}
Starting with section 4 line 3
An expression e can be implicitly converted to a type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t (8.5). Certain language constructs require that an expression be converted to a Boolean value. An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t (8.5). The effect of either implicit conversion is the same as performing the declaration and initialization and then using the temporary variable as the result of the conversion. The result is an lvalue if T is an lvalue reference type (8.3.2), and an rvalue otherwise. The expression e is used as an lvalue if and only if the initialization uses it as an lvalue.
Following that I found the section on initializers related to user-defined types in 8.5 line 6
If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.
Finally I ended up at 12.3 line 2 about user-defined conversions which states
User-defined conversions are applied only where they are unambiguous (10.2, 12.3.2).
Needless to say, 10.2 and 12.3.2 didn't answer my question.
- Can someone shed some light on what effect
const
has on implicit conversions? - Does the use of
const
make the conversion "unambiguous" per 12.3 line 2? - Does
const
somehow affect lvalue vs. rvalue talked about in section 4?