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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:33:39+00:00 2026-05-22T15:33:39+00:00

struct A {}; struct B { B (A* pA) {} B& operator = (A*

  • 0
struct A {}; 
struct B
{
  B (A* pA) {}
  B& operator = (A* pA) { return *this; }
};

template<typename T>
struct Wrap
{
  T *x; 
  operator T* () { return x; }
};

int main ()
{
  Wrap<A> a;
  B oB = a; // error: conversion from ‘Wrap<A>’ to non-scalar type ‘B’ requested
  oB = a;  // ok
}

When oB is constructed then Why B::B(A*) is NOT invoked for Wrap<T>::operator T () ? [Note: B::operator = (A*) is invoked for Wrap<T>::operator T () in the next statement]

  • 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-22T15:33:39+00:00Added an answer on May 22, 2026 at 3:33 pm

    The problem is that the number of user-defined conversions that are invoked implicitly is limited (to 1) by the Standard.

    B ob = a;
    

    implies two user conversions:

    • on a: Wrap<A>::operator A*() should be called
    • on the result: B::B(A*) should be called

    @James Kanze’s explanation: this syntax is called “copy initialization”, effectively equivalent to B ob = B(a) (with the copy being elided most of the time). This is different from B ob(a) which is a “direct initialization” and would have worked.

    if you explicitly qualify any of this, it will work, for example:

    B ob = B(a);
    

    On the other hand, for the second case there is no issue:

    ob = a;
    

    is short-hand for:

    ob.operator=(a);
    

    And thus only one user-defined conversion is required, which is allowed.

    EDIT:

    Since it’s been required in a comment (to Kirill’s answer) we can take a guess at the motive.

    Chained conversions could be long, very long, and therefore:

    • could surprise users — implicit conversions may already be surprising as it is…
    • could lead to an exponential search of the possibilities (for the compiler) — it would need to go from both ends, trying to check all possible conversions, and somehow “join” the two (with the shortest path possible).

    Furthermore, as long as there is more than 1 conversion, you run into the risk of having cycles, which would have to be detected (even though diagnostic would probably not be required, and be subject to Quality Of Implementation).

    So, since a limit is necessary to avoid infinitely long searches (it could have been left unspecified, with a minimum required), and since beyond 1 we may have new issues (cycles), then 1 seems as good a limit as any after all.

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

Sidebar

Related Questions

template<typename T> struct A { A<T> operator%( const T& x); }; template<typename T> A<T>
I'm confused... why isn't my assignment operator getting called here? template<typename This> struct mybase
I have two structs: template <typename T> struct Odp { T m_t; T operator=(const
I have a class A: template <typename T, int I> struct A {}; and
struct Div { int i; int j; }; class A { public: A(); Div&
I have some null struct, for example: struct null_type { null_type& someNonVirtualMethod() { return
struct elem { int i; char k; }; elem user; // compile error! struct
I have the following code: template<typename Parent, typename T, void (Parent::*Setter)(T), T (Parent::*Getter)()> struct
I have this class for double linked lists: template <typename T> class Akeraios {
Let's say we have template <typename T> struct Foo {}; and struct Bar {

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.