Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3234376
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:23:03+00:00 2026-05-17T17:23:03+00:00

After extensive reading of ISO/IEC 14882, Programming language – C++ I’m still unsure why

  • 0

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.

  1. Can someone shed some light on what effect const has on implicit conversions?
  2. Does the use of const make the conversion “unambiguous” per 12.3 line 2?
  3. Does const somehow affect lvalue vs. rvalue talked about in section 4?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:23:04+00:00Added an answer on May 17, 2026 at 5:23 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading lots of blogs, forum entries and several Apple docs, I still don't
After reading the Key-Value Coding Programming Guide , the Key-Value Observing Programming Guide and
So after extensive research and tons of jQuery and Javascript solutions I simply could
After an extensive search I have been unable to find any information on this
After some time researching and trying different things I still cannot get my @ExceptionHandler
After having checked extensively for an answer to this question, I still cannot find
After reading Skeet's article , I went with the general advice to ..consider renaming
After reading How do I protect Python code? , I decided to try a
after a lot of googling and reading several solutions on how to manage mixed
After reading the following article Create Test Conditions for the Database Unit Test Designer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.