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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:12:53+00:00 2026-05-26T11:12:53+00:00

I have done a little experiment and I don’t understand the output ! class

  • 0

I have done a little experiment and I don’t understand the output !

class C {
public:
  operator int() const { std::cout << "I'm const" << std::endl;}
  operator int(){ std::cout << "I'm not const" << std::endl;}
};

void f(int){};

int main()
{
  f(C());
}

Why does this output "i'm not const" ? Shouldn’t the first cast of the C object, which is an rvalue and therefore const, be prioritary?

Thanks ! 🙂

Edit : If it can make the question more precise :

In constrast :

void g(C const &){ std::cout << "I take a const" << std::endl; };
void g(C &){ std::cout << "I take a non const" << std::endl; };

g(C()) outputs “I take a const”.

  • 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-26T11:12:53+00:00Added an answer on May 26, 2026 at 11:12 am

    A temporary isn’t constant:

    C();  // not const
    

    If you want to refer to it constantly, cast it:

    f(static_cast<const C&>(C()));
    

    The point is that the object whose member function (here the conversion operator) is called is not constant. This has nothing to do with the result of the conversion.

    The result isn’t actually required to be constant, either. Suppose we add another class:

    class D { };
    
    class C {
      // ...
      operator D() { return D(); }
    };
    
    int main()
    {
      D d;
      static_cast<D>(C()) = d;      // OK: assign d to the result of the conversion
      // static_cast<int>(C()) = 6; // Error: left-hand side is not an lvalue
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have done very little with encoding of Text. Truthfully, I don't really even
I have done some digging out here and am still a little unsure how
I have not done any large game development projects, only messed around with little
I have done a little googling but not found too much info on the
It's been a little while since I have done some hard out coding (since
I have done a little research around the matter, but haven't really been able
I have a background in programming and have done a little .NET but I
I have been doing ASP.NET for a while, and have done very little in
I have done a little research here on SO and haven't found exactly what
I am just starting with node.js. I have done a little ajax stuff but

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.