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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:23:31+00:00 2026-05-24T13:23:31+00:00

If I compile (gcc 4.6.0) and run this code: #include <iostream> template <typename T>

  • 0

If I compile (gcc 4.6.0) and run this code:

#include <iostream>

template <typename T> void F(/* const */ T& value) {
    std::cout << "T & " << value << std::endl;
}

template <typename T> void F(/* const */ T* value) {
    std::cout << "T * " << value << std::endl;
    F(*value);
}

int main(int argc, char* argv[]) {
    float f = 123.456;   
    float* pf = &f;

    F(pf);

    return 0;
}

I get the following output:

T * 0x7fff7b2652c4
T & 123.456

If I uncomment the const keywords I get the following output:

T & 0x7fff3162c68c

I can change float* pf = &f; to const float* pf = &f; to get the original output again, that’s not the issue.

What I’d like to understand is why, when compiling with the const modifiers, overload resolution considers const T& value a better match than const T* valuefor a non-const float*?

  • 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-24T13:23:32+00:00Added an answer on May 24, 2026 at 1:23 pm

    During overload resolution, overloads requiring no conversions beat overloads requiring some conversions, even if those conversions are trivial. Quoting the C++03 standard, [over.match.best] (§13.3.3/1):

    Define ICSi(F) as follows:

    • if F is a static member function, ICS1(F) is defined such that ICS1(F) is neither better nor worse than ICS1(G) for any function G, and, symmetrically, ICS1(G) is neither better nor worse than ICS1(F); otherwise,
    • let ICSi(F) denote the implicit conversion sequence that converts the i-th argument in the list to the type of the i-th parameter of viable function F. 13.3.3.1 defines the implicit conversion sequences and 13.3.3.2 defines what it means for one implicit conversion sequence to be a better conversion sequence or worse conversion sequence than another.

    Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then

    • for some argument j, ICSj(F1) is a better conversion sequence than ICSj(F2), or, if not that,
    • F1 is a non-template function and F2 is a function template specialization, or, if not that,
    • F1 and F2 are function template specializations, and the function template for F1 is more specialized than the template for F2 according to the partial ordering rules described in 14.5.5.2, or, if not that,
    • the context is an initialization by user-defined conversion (see 8.5, 13.3.1.5, and 13.3.1.6) and the standard conversion sequence from the return type of F1 to the destination type (i.e., the type of the entity being initialized) is a better conversion sequence than the standard conversion sequence from the return type of F2 to the destination type.

    When const is present, in order to call the overload taking a reference, no conversion is necessary — T is deduced to be float* and the argument is float* const&. However, in order to call the overload taking a pointer, float would need to be converted to float const for said overload to be viable. Consequently, the overload taking a reference wins.

    Note, of course, that if pf were changed to be a float const*, the behavior would go back to the way you expected because the overload taking a pointer would no longer require a conversion.

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

Sidebar

Related Questions

The following code doesn't compile with gcc, but does with Visual Studio: template <typename
The following fails to compile (with gcc 4.2.1 on Linux, anyway): template< typename T
This code run on Turbo C but not on gcc compiler Error:syntax error before
i am trying to compile this code, but if i do using: gcc prt.c
When I run the following code... #ifndef KEYEDITEM_H_INCLUDED #define KEYEDITEM_H_INCLUDED #include <string> typedef std::string
Why does the following code run? #include <iostream> class A { int num; public:
see i have code like #include<stdio.h> #include<pthread.h> #include<string.h> void* thread_function(void) { printf (This is
stattest.c: // compile: gcc -o stattest stattest.c #include <stdio.h> #include <sys/stat.h> int main(int argc,
In gcc I can do compile-time warnings like this: #if !defined(_SOME_FEATURE_) #warning _SOME_FEATURE_ not
I am learning NASM and am tying to compile this code (which I found

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.