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

  • Home
  • SEARCH
  • 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 3804432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:31:02+00:00 2026-05-19T14:31:02+00:00

Before continue reading this, please read Is there a difference in C++ between copy

  • 0

Before continue reading this, please read Is there a difference in C++ between copy initialization and direct initialization? first, make sure you understand what it is talking about.

I’ll summarize the rule here first (read standard n3225 8.5/16, 13.3.1.3, 13.3.1.4, and 13.3.1.5),

1) For direct initialization, all constructors will be considered as the overloading set, the overloading resolution will select the best one according to the overloading resolution rules.

2) For copy initialization and the source type is the same as destination type or derived from destination type, the rule is as same as above except that only converting constructors (constructors without explicit) will be considered as the overloading set. This actually means explicit copy/move constructors will not be considered into the overloading set.

3) For copy initialization cases not included in (2) above (source type is different from destination type and not derived from destination type), we first consider user-defined conversion sequences that can convert from the source type to the destination type or (when a conversion function is used) to a derived class thereof. If the conversion succeeds, the result is used to direct initialize the destination object.

3.1) During this user-defined conversion sequence, both converting ctors (non-explicit ctors) and non-explicit conversion functions will be considered, according to rules in 8.5/16 and 13.3.1.4.

3.2) The result prvalue will direct initialize the destination object, as rules listed in (1), see 8.5/16.

Okay, enough for rules, let’s look at some weird code, which I really have no idea on where my reasoning is wrong, or just all compilers are wrong. Please help me, thanks.

struct A
{
    A (int) { }
    A() { }
    explicit A(const A&) { }
};
struct B
{
    operator A() { return 2; }
    //1) visual c++ and clang passes this
    //gcc 4.4.3 denies this, says no viable constructor available
};
int main()
{
    B b;
    A a = b;
    //2) oops, all compilers deny this
}

In my understanding, for (1),

operator A() { return 2; }

Because C++ has a rule that function return is taken as copy-initialization, according to the rule above, 2 will be firstly implicitly converted to A, which should be OK because A has a constructor A(int). Then the converted temporary prvalue will be used to direct-initialize the returned object, which should be OK too because direct-initialization can make use of the explicit copy constructor. So GCC is wrong.

For (2),

A a = b;

In my understanding, firstly b is implicitly converted to A, by operator A(), and then the converted value shall be used to direct-initialize a, which can of course call the explicit copy constructor? Thus this should pass compilation and all compilers are wrong?

Note that for (2), both visual c++ and clang has an error similar to,
“Error, cannot convert from B to A”, but if I remove the explicit keyword in the copy constructor of A, the error is gone..

Thanks for reading.


edit 1

Because somebody still didn’t get what I meant, I quote the following standard from 8.5/16,

Otherwise (i.e., for the remaining
copy-initialization cases),
user-defined conversion sequences that
can convert from the source type to
the destination type or (when a
conversion function is used) to a
derived class thereof are enumerated
as described in 13.3.1.4, and the best
one is chosen through overload
resolution (13.3). If the conversion
cannot be done or is ambiguous, the
initialization is ill-formed. The
function selected is called with the
initializer expression as its
argument; if the function is a
constructor, the call initializes a
temporary of the cv-unqualified
version of the destination type. The
temporary is a prvalue. The result of
the call (which is the temporary for
the constructor case) is then used to
direct-initialize, according to the
rules above, the object that is the
destination of the
copy-initialization. In certain cases,
an implementation is permitted to
eliminate the copying inherent in this
direct-initialization by constructing
the intermediate result directly into
the object being initialized; see
12.2, 12.8.

Note that it did mention direct initialize after the user-defined conversion. Which means, in my understanding, the following code shall obey the rules as what I commented, which is confirmed by both clang, coomeau online, visual c++, but GCC 4.4.3 fails both (1) and (2). Although this is a weird rule, but it follows the reasoning from the standard.

struct A
{
    A (int) { }
    A() { }
    explicit A(const A&) { }
};

int main()
{
    A a = 2;    //1)OK, first convert, then direct-initialize
    A a = (A)2; //2)oops, constructor explicit, not viable here!
}
  • 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-19T14:31:02+00:00Added an answer on May 19, 2026 at 2:31 pm

    You declared your copy constructor explicit (BTW, why?), which means that it can no longer be used for implicit copying of class objects. In order to use this constructor for copying you are now forced to use direct-initialization syntax. See 12.3.1/2

    2 An explicit constructor constructs objects just like non-explicit constructors, but does so only where the direct-initialization syntax (8.5) or where casts (5.2.9, 5.4) are explicitly used.

    The issue can be illustrated by the following much shorter example

    struct A {
      A() {}
      explicit A(const A&) {}
    };
    
    int main() {
      A a;
      A b = a; // ERROR: copy-initialization
      A c(a); // OK: direct-initialization
    }
    

    This is what blocks all of your conversions from working, since all of them rely on copy-initialization, which in turns relies on implicit copying. And you disabled implicit copying.

    Additionally, see the Defect Report #152 which covers this specific issue. Although I’m not sure what the consequences of the “proposed resolution” are supposed to be…

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

Sidebar

Related Questions

While I realize there is a similar question ( How to serialize an Exception
Is it possible to require a logged in user to complete registration before he
I just read amending a single file in a past commit in git but
I used if statements before in a the success function of an ajax call
I need the following functionality in my method: if the method is called before
I want my application to check (before start installation) for Nokia_PC_Suite_rel_7_0_8_2 my steps are
I am using the following code to countdown before enabling a button. <script type=text/javascript>
For a class assignment, we are writing a custom syscall which pulls certain information
I want to layout a button with Drawable image on the top of it
I have an app that needs to scan a barcode to get a code

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.