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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:03:39+00:00 2026-05-14T09:03:39+00:00

Regarding the below code, how does the compiler choose which template function to call?

  • 0

Regarding the below code, how does the compiler choose which template function to call?
If the const T& function is omitted, the T& function is always called.
If the T& function is omitted, the const T& function is always called.
If both are included, the results are as below.

#include <iostream>
#include <typeinfo>

template <typename T>
void function(const T &t)
{
    std::cout << "function<" << typeid(T).name() << ">(const T&) called with t = " << t << std::endl;
}

template <typename T>
void function(T &t)
{
    std::cout << "function<" << typeid(T).name() << ">(T&) called with t = " << t << std::endl;
}

int main()
{
    int i1 = 57;
    const int i2 = -6;

    int *pi1 = &i1;
    int *const pi3 = &i1;
    const int *pi2 = &i2;
    const int *const pi4 = &i2;

    function(pi1); ///just a normal pointer -> T&
    function(pi2); ///cannot change what we point to -> T&
    function(pi3); ///cannot change where we point -> const T&
    function(pi4); ///cannot change everything -> const T&

    return 0;
}

/* g++ output: 
function<Pi>(T&) called with t = 0x22cd24
function<PKi>(T&) called with t = 0x22cd20
function<Pi>(const T&) called with t = 0x22cd24
function<PKi>(const T&) called with t = 0x22cd20
*/

/* bcc32 output: 
function<int *>(T&) called with t = 0012FF50
function<const int *>(T&) called with t = 0012FF4C
function<int *>(const T&) called with t = 0012FF50
function<const int *>(const T&) called with t = 0012FF4C
*/

/* cl output: 
function<int *>(T&) called with t = 0012FF34
function<int const *>(T&) called with t = 0012FF28
function<int *>(const T&) called with t = 0012FF34
function<int const *>(const T&) called with t = 0012FF28
*/
  • 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-14T09:03:40+00:00Added an answer on May 14, 2026 at 9:03 am

    Here is a brief summary of the process the compiler goes through. It doesn’t cover everything, but it gets you started.

    In this case, the decision is made the same as a non-templated function. Given void f(int&) and void f(const int&), the first will be chosen for regular ints, and the second for const ints. The parameters simply match the inputs better this way: if you provide a variable you can modify, it calls a function that can modify them, if you provide a variable you can not modify, it calls a function that can not modify them.

    In your sample code, pi2, being declared as a const int *, is a non-constant pointer to constant data. So within your function, you can change t, but not *t. By contrast, pi3 is a constant pointer to non-constant data. So you can change *t but not t.

    If you changed your code slightly:

    function(*pi1);
    function(*p12);
    function(*pi3);
    function(*pi4);
    

    In this case, the first and third would both resolve to the T& version, because *pi1 and *pi3 are both of type int& and can therefore be modified. *pi2 and *pi4 are both const int&, so they resolve to the const T& overload.

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

Sidebar

Ask A Question

Stats

  • Questions 386k
  • Answers 386k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You don't show enough code to determine whether the size… May 14, 2026 at 11:52 pm
  • Editorial Team
    Editorial Team added an answer This is a tricky one. There is no way to… May 14, 2026 at 11:52 pm
  • Editorial Team
    Editorial Team added an answer I usually do something like <form action="/controller/action" method="post" id="formID"> </form>… May 14, 2026 at 11:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.