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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:04:10+00:00 2026-06-09T03:04:10+00:00

Possible Duplicate: Functions with const arguments and Overloading I am pretty confused by the

  • 0

Possible Duplicate:
Functions with const arguments and Overloading

I am pretty confused by the overloading and const declaration rules.
Here are two things that puzzle me maybe you can help me find the deeper misunderstanding in my head that result in them being puzzling to me. 😉

First issue:

My compiler allows this:

void f(int & x) {
  std::cout << "plain f" << std::endl;
}
void f(const int & x) {
  std::cout << "const f" << std::endl;
}

But the following causes a compile error (function already has a body):

void f(int x) {
  std::cout << "plain f" << std::endl;
}
void f(const int x) {
  std::cout << "const f" << std::endl;
}

Which I suppose makes sense because I thought the const was only there to tell the compiler that the object being passed is not changed and in the second case it is copied anyway. But if that is correct then why can I overload functions using const?

In other words, why if I use the compiling version and call the functions like this:

  int x1 = 5;
  const int x2 = 5;
  f(x1);
  f(x2);

do I get “plain f” and “const f” instead of “const f” twice? Apparently now I am also using the const to tell the compiler which function to call not only that the reference doesn’t change. This gets more confusing because if I remove the “plain” version it works just fine and calls the “const” version twice.

Now what is my actual question? I would like to know what the ideas behind this behavior are because otherwise memorizing it is very hard.

  • 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-09T03:04:11+00:00Added an answer on June 9, 2026 at 3:04 am

    I thought the const was only there to tell the compiler that the object being
    passed is not changed and in the second case it is copied anyway

    You are correct. Because in the second case it’s copied anyway, and so the const makes no difference to the caller, the standard defines that void f(const int x) and void f(int x) have the same signature. Hence they collide, you’re trying to define the same function twice.

    Because in the first case it isn’t copied anyway, void f(const int &x) and void f(int &x) have different signatures. Hence they overload.

    In your first case, it’s forbidden to call the int& version of f with x2 as argument, because that would create a non-const reference to a const object without any explicit cast in sight. Doing that defeats the purpose of the const system (which is that if you want to break const-safety, you have to do so explicitly with a cast). So it makes sense to have const- and non-const overloads of functions with reference parameters.

    In your second case, there’s no relation between the const-ness of the source of a copy, and the const-ness of the destination. You can initialize a const variable from a non-const one, or a non-const one from a const one. This causes no problems and doesn’t break const-safety. That’s why the standard helpfully makes this clear by defining that your two “different” versions of f are actually the same function.

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

Sidebar

Related Questions

Possible Duplicate: Method overloading in Objective-C? Is method overloading not possible. I have two
Possible Duplicate: Virtual Functions and Performance C++ Is this correct, that class member function
Possible Duplicates: What's the use of const here Using 'const' in class's functions Hi
Possible Duplicate: Meaning of “const” last in a C++ method declaration? In the below
Possible Duplicate: Why must the copy assignment operator return a reference/const reference? Operator= overloading
Possible Duplicate: What are the basic rules and idioms for operator overloading? Operator overloading
Possible Duplicate: Returning unique_ptr from functions 20.7.1.2 [unique.ptr.single] defines copy constructor like this :
Possible Duplicate: How do you trigger javascript functions from flash? Possible Duplicate: How do
Possible Duplicate: True random number generator I have worked with random functions in python,ruby,
Possible Duplicate: Create shortcut to console.log() In javascript we can easy assign functions to

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.