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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:34:23+00:00 2026-05-23T09:34:23+00:00

Following is the test code: struct A { operator int (); operator int ()

  • 0

Following is the test code:

struct A
{
  operator int ();
  operator int () const;
};

void foo (const int);

Now, upon invoking:

foo(A());  // calls A::operator int()

Why does it always chooses the non-const version ? Even making operator const int () const; doesn’t have any effect on invoking foo(). Apart from standard reference, can someone explain logically, the reason behind it ?

  • 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-23T09:34:24+00:00Added an answer on May 23, 2026 at 9:34 am

    A() gives you a temporary A object that is not const-qualified. The A() expression is an rvalue expression, yes, but that does not make the A object const-qualified.

    Since the A object is not const-qualified, the non-const operator int() is an exact match and the const operator int() requires a qualification conversion, so the non-const overload is selected as the best match.

    If you want it to be const-qualified, you’d need to explicitly request a const-qualified A:

    foo(identity<const A>::type());
    

    where identity is defined as

    template <typename T>
    struct identity { typedef T type; };
    

    Note that there is really no difference between operator const int() const and operator int() const: the result is an rvalue and only class-type rvalues can be const-qualified (int is not a class type).

    Note also that there is no difference between the void foo(const int) that you have and void foo(int). Top-level const-qualifiers on parameter types do not affect the type of the function (i.e., the type of both of those declarations is void foo(int)). Among other reasons, this is because it doesn’t matter to the caller whether there is a top-level const-qualifier; it has to make a copy regardless. The top-level const-qualifier affects only the definition of the function.

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

Sidebar

Related Questions

I have the following test-code: CREATE TABLE #Foo (Foo int) INSERT INTO #Foo SELECT
Please consider the following code. struct foo { }; template<typename T> class test {
I have the following test code use Data::Dumper; my $hash = { foo =>
Test the following code: #include <stdio.h> #include <stdlib.h> main() { const char *yytext=0; const
The following code using System.Threading; class Test { volatile int counter = 0; public
For the following code: #include<iostream> #include<vector> #include<string> using namespace std; struct Test { string
I have the following test code #include <iostream> template <typename T> struct PS {
What is wrong with the following C++11 code: struct S { int a; float
I have following piece of code: class Test{ private: int id; public: Test(int v):id(v)
Say I have the following code: struct test* t1; t1 = get_t(1); ... where

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.