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 6334459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:39:45+00:00 2026-05-24T18:39:45+00:00

The C++ Standard Library by Nicolai M. Josuttis states: There is a minor difference

  • 0

The C++ Standard Library by Nicolai M. Josuttis states:

There is a minor difference between

X x;
Y y(x) //explicit conversion

and

X x;
Y y = x; //implicit conversion

Following to say: “The former creates a new object of type Y by using an explicit conversion from type X, whereas the latter creates a new object of type Y by using an implicit conversion.”

I’m a little confused about the concepts of explicit vs implicit conversion I guess. In both cases you’re taking an X and pushing it into a Y per se – one uses a Y’s constructor and one uses the assignment operator though.

What’s the difference in how the conversion is treated in these two cases, what makes it explicit/implicit, and how does this tie into making a class constructor defined with the “explicit” key word, if at all?

  • 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-24T18:39:47+00:00Added an answer on May 24, 2026 at 6:39 pm

    one uses a Y’s constructor and one uses the assignment operator though.

    Nope. In the second case it’s not an assignment, it’s an initialization, the assignment operator (operator=) is never called; instead, a non-explicit one-parameter constructor (that accepts the type X as a parameter) is called.

    The difference between initialization and assignment is important: in the first case, a new object is being created, and it starts its life with the value that it is being initialized with (hence why a constructor is called), while assignment happens when an object is assigned (~copied) to an object that already exists and already is in a definite state.

    Anyway, the two forms of initialization that you wrote differ in the fact that in the first case you are explicitly calling a constructor, and thus any constructor is acceptable; in the second case, you’re calling a constructor implicitly, since you’re not using the “classical” constructor syntax, but the initialization syntax.

    In this case, only one-parameter constructors not marked with explicit are acceptable. Such constructors are called by some people “converting” constructors, because they are involved in implicit conversions.

    As specified in this other answer, any constructor not marked as explicit can take part in an implicit conversion for e.g. converting an object passed to a function to the type expected by such function. Actually, you may say that it’s what happens in your second example: you want to initialize (=create with a value copied from elsewhere) y with x, but x first has to be converted to type Y, which is done with the implicit constructor.

    This kind of implicit conversion is often desirable: think for example to a string class that has a converting (i.e. non-explicit) constructor from a const char *: any function that receives a string parameter can also be called with a “normal” C-string: because of the converting constructor the caller will use C-strings, the callee will receive its string object.

    Still, in some cases one-parameters constructors may not be appropriate for conversion: usually this happens when their only parameter is not conceptually “converted” to the type of the object being created, but it is just a parameter for the construction; think for example about a file stream object: probably it will have a constructor that accepts the name of the file to open, but it makes no sense to say that such string is “converted” to a stream that works on that file.

    You can also find some more complex scenarios where these implicit conversions can completely mess-up the behavior that the programmer expects from overload resolution; examples of this can be found in the answers below the one I linked above.

    More simply, it can also happen that some constructors may be very heavyweight, so the class designer may want to make sure that they are invoked explicitly. In these cases, the constructor is marked as explicit, so it can be used only when called “explicitly as a constructor” and doesn’t take part in implicit conversions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In his book The C++ Standard Library (Second Edition) Nicolai Josuttis states that lambdas
While playing with standard library i've found a strange difference between python2 and python3.
Nicolai Josuttis, in his book The C++ Standard Library - A Tutorial and Reference,
in book The Cpp standard library, 2nd edition, by Nicolai M. Josuttis, says (5.4,
The C++ Language Standard states the following concerning template components in the Standard Library:
Is there a Fortran standard library (I keep hearing about something called ISO_C_BINDING, but
Is there a standard library function to check that a string S is of
Using the sqlite3 standard library in python 2.6.4, the following query works fine on
Is there a reference about C++ Standard Library Exceptions? I just want to know
I'm reading through: The C++ Standard Library: A Tutorial and Reference by Nicolai M.

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.