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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:38:13+00:00 2026-06-08T09:38:13+00:00

struct B { }; struct A { operator A&() const; operator B&() const; };

  • 0
struct B 
{

};

struct A
{
    operator A&() const;
    operator B&() const;
};


int main()
{
    const A a;
    B& br = a;
    A& ar = a;  
}

Why can I create cast operator to B&, but not to A&.

May be it does not have much sense (one can use it to erase const modifier, as in example), but it at least inconsistent!

  • 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-06-08T09:38:15+00:00Added an answer on June 8, 2026 at 9:38 am

    You can’t do this because it’s explicitly forbidden. N3290 § 12.3.2 states:

    Such functions are called
    conversion functions. No return type can be specified. If a conversion function is a member function, the
    type of the conversion function (8.3.5) is “function taking no parameter returning conversion-type-id”. A
    conversion function is never used to convert a (possibly cv-qualified) object to the (possibly cv-qualified)
    same object type (or a reference to it)
    , to a (possibly cv-qualified) base class of that type (or a reference to
    it), or to (possibly cv-qualified) void.

    (Emphasis mine)

    This is discussed further in a note:

    These conversions are considered as standard conversions for the purposes of overload resolution (13.3.3.1, 13.3.3.1.4) and
    therefore initialization (8.5) and explicit casts (5.2.9).

    Which explains this decision – it would interfere with the built-in mechanics too much. (For little gain).

    If you really want something non-const from a const object the only smart way to do this is constructing a new instance using the copy constructor.

    As a work around you could introduce a lightweight intermediary (like a smart pointer):

    struct B {};
    
    struct A {};
    
    namespace {
      B b_inst;
      A a_inst;
    }
    
    struct A_wrapper {
      A& inst;
      // This is perfectly fine: const alters the reference, not what it refers to
      operator A&() const { return inst; }
      operator B&() const { return b_inst; }
      A_wrapper() : inst(a_inst) {}
    };
    
    int main() {
      const A_wrapper a;
      B& br = a;
      A& ar = a;
    }
    

    But really, wanting to do this in the first place looks like a code smell.

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

Sidebar

Related Questions

Consider the following code: struct Foo { Foo operator+(const Foo &rhs) const; // notice
I have the following code: template <class T> struct pointer { operator pointer<const T>()
I have this code: #include <iostream> #include <functional> struct A { int operator()(int i)
struct Node { int value Node* next; } typedef List Node* const Set operator
struct Foo { int data; Foo() = default; Foo(const Foo& arg) = default; };
struct MemBlock { char mem[1024]; MemBlock operator*(const MemBlock &b) const { return MemBlock(); }
I have something like this: struct D { virtual void operator() {...}; } struct
#include <iostream> using namespace std; struct Y; struct X { X(const Y&) { cout
I have the following code: struct simple { simple (int a1, int a2) :
The following code does not compile. int a = 1, b = 2, c

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.