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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:05:44+00:00 2026-06-15T09:05:44+00:00

I had defined two overloaded functions, their declarations are as follows template <class T>

  • 0

I had defined two overloaded functions, their declarations are as follows

template <class T> void Foo(const T *p);    // lets call this Foo_p

template <class T> void Foo(const T& r);    // lets call this Foo_r

When I call

Foo( ptr_to_non_const );

Foo_r gets called. I assumed that constant qualification are stripped away from pointers when looking for best match. Since T* is more specialized than T& I expected that Foo_p will be called.

Could anyone point me to a good resource that lists template argument deduction rules and the precedence of matches.

In this particular case I intend template <class T> void Foo(const T& r) to be called for non-pointer type. Does it mean I have to define the functions with and without const qualifications. For one argument it is not a big deal, but my function take more than one pointer, so I would want to avoid the repition. Any suggestions would be appreciated.

  • 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-15T09:05:45+00:00Added an answer on June 15, 2026 at 9:05 am

    The template deduction rules are quite complicated and I’m not sure if there is anywhere a simple summary. However, if two templates are candidates and one of them requires a conversion while the other does not, the one not requiring a conversion to produce the template argument is chosen. In your example, the matching instantiations are thus:

    S* ptr_to_non_const = ...;
    Foo(ptr_to_non_const); // => candidates:
                           //   a. F(const T&) with `T` deduced as `S*` requires no
                           //      conversion
                           //   b. F(const T*) with `T` deduced as `S` requires `S*` to
                           //      `S const*` conversion
    

    To force the use of the pointer overload, you can remove the reference version from the overload set using std::enable_if<...> together with std::is_pointer<...>:

    template <class T>
    typename std::enable_if<!std::is_pointer<T>::value>::type
    Foo(const T& r);
    

    Although this uses the C++ 2011 features, both std::enable_if<...> and std::is_pointer<...> can be implemented quite easy with a C++ 2003 compiler, as well (and I’m pretty sure that Boost has done so).

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

Sidebar

Related Questions

I have defined a class (call it class A) in C# and overloaded some
In the code below, I've defined two overloaded methods named Bar . In Foo()
I had a connection string to a MS Access DB file, Foo.accdb, defined and
I had a model that had an unrelated class defined inside of it. I
I am trying to understand some C++ syntax: template<class T> class Foo { Foo();
I have two tables, for simplicity lets pretend they're defined like this Stack {
Previously, I had my LegNo enums defined simply as: NO_LEG, LEG_ONE, LEG_TWO and by
If you had two snippets: (global-set-key \C-d delete-char) and (define-key global-map \C-d delete-char) Is
If I had two tables, users and blocked_users , how would I define the
Please consider two restrictions - Can't move MyProperty to interface or abstract class. FooEventHandler

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.