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

The Archive Base Latest Questions

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

Given a template and a more specialized overload: template <typename T> const T& some_func(const

  • 0

Given a template and a more specialized overload:

template <typename T>
const T& some_func(const T& a, const T& b) 
{
    std::cout << "Called base template\n";
    return (a < b) ? a : b; 
}

template <typename T>
T* const& some_func(T* const& a, T* const& b) 
{
    std::cout << "Called T* overload\n";
    return (*a < *b) ? a : b; 
}

Then the following works as expected:

int main()
{
    std::cout << some_func(5.3, 6.2) << "\n";
    double a = 1;
    double b = 2;
    double *p = &a;
    double *q = &b;
    const double *ret = some_func(p, q); 
    std::cout << *ret << "\n";
    return 0;
} 

With the first printing Called base template, the second printing Called T* overload. If we replace the overload signature with:

template <typename T>
const T*& some_func(const T*& a, const T*& b)

then the second call now calls the base template. Given that int const& x is equivalent to const int& x, am I incorrect in assuming T* const& is equivalent to const T*&? Why is the first version resolved properly while the second is not?

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

    Yes you are incorrect. In const T*& T is const, in T* const& the pointer (T*) is const.

    The change happens when you shift const to the right of *. const T*& and T const *& are equivalent but T* const& is different. Typedefs can help, given

    typedef T *TPtr;
    

    const TPtr& is equivalent to TPtr const & is equivalent to T* const &.

    Declarators in C/C++ are hard to parse (for a human). Even the inventor of C said so.

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

Sidebar

Related Questions

So given the following template functions with partial specialization template<typename T> void foo(vector<T> &in)
Given the following template: template <typename T> class wrapper : public T {}; What
Given a class with a member template function like this one: template <typename t>
Given the following piece of code: template<typename T> class MyContainer { typedef T value_type;
Given a C++ template class (or function) definition: template<typename T> struct Foo { T
Given a template class like this: template <typename T> class C { T member;
Given: template<class T> struct test { void foo(int b); void testFunc( int a )
Given a template template <int n> void f(){...}; I know I can specialize it
I have the given view and template code, when i try to run this
Given that sorl isn't an app directory wide and the template tag definition lives

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.