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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:35:56+00:00 2026-06-11T04:35:56+00:00

It’s generally a bad idea to overload a function template taking a T&& parameter,

  • 0

It’s generally a bad idea to overload a function template taking a “T&&” parameter, because that can bind to anything, but let’s suppose we do it anyway:

template<typename T>
void func(const T& param)
{
  std::cout << "const T&\n";
}

template<typename T>
void func(T&& param)
{
  std::cout << "T&&\n";
}

My understanding has been that the const T& overload will get called for arguments that are const lvalues, and the T&& overload will get called for all other argument types. But consider what happens when we call func with arrays of const and non-const contents:

int main()
{
  int array[5] = {};
  const int constArray[5] = {};

  func(array);             // calls T&& overload
  func(constArray);        // calls const T& overload
}

VC10, VC11, and gcc 4.7 agree on the results shown. My question is why the second call invokes the const T& overload. The simple answer is that constArray has a const in it, but I think that’s too simple. The type T that is deduced (regardless of the template selected) is “array of 5 const ints” so the type of param in the const T& overload would be “reference to const array of 5 const ints”. But the array named constArray is not itself declared const. So why doesn’t the call to func(constArray) invoke the T&& overload, thus yielding a type for param of “reference to array of 5 const ints”?

This question is motivated by the discussion associated with the question at c++ template function argument deduce and function resolution, but I think that thread got sidetracked on other issues and did not clarify the question I’m now asking here.

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

    In function parameter lists (as well as everywhere else), a cv qualification on an array type is shuffled right to qualify the array element type. For example, with T = int [5], const T & is converted to int const (&) [5].

    3.9.3 CV-qualifiers [basic.type.qualifier]

    2 – […] Any cv-qualifiers applied to an array type affect the array element type, not the array type (8.3.4).

    So the call to func with an argument of type int const [5] is deduced as a call to either of:

    void func<int [5]>(int const (&) [5])
    void func<int const (&) [5]>(int const (& &&) [5])
    // where the above collapses to
    // 'void func<int const (&) [5]>(int const (&) [5])'
    

    Both overloads are viable, but the former is preferred:

    Let T1 be the const T & template and T2 be the T && template; that is, their parameter types are T1 := const T & and T2 := T &&. Then the transformed argument types (14.5.6.2:3) can be written A1 := const C &, A2 := D && for synthesized types C, D.

    Now, we attempt to order T1 against T2 (14.8.2.4:2), first using A1 as the argument template and P2 as the parameter template. We remove references (14.8.2.4:5) giving A1 -> const C and T2 -> T, then remove cv-qualification (14.8.2.4:7) giving A1 -> C and T2 -> T. The template T can be deduced to C (14.8.2.4:8) so A1 is at least as specialised as P2; conversely, A2 -> D -> D, P1 -> const T -> T, and T can be deduced to D, so A2 is at least as specialised as P1.

    This would usually imply that neither is more specialised than the other; however, because the P and A types are reference types 14.8.2.4:9 applies, and since A1 is an lvalue reference and P2 is not, T1 is considered more specialized than T2. (A tie between reference types can also be broken by cv-qualification under the same clause.)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I need a function that will clean a strings' special characters. I do NOT
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this

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.