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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:26:24+00:00 2026-05-16T11:26:24+00:00

The following small example shows my problem: template<class T> struct X { static void

  • 0

The following small example shows my problem:

template<class T> struct X
{
    static void xxx(T& x) { }
    static void xxx(T&& x) { }
};

int main(int argc, char** argv)
{
    int x = 9;
    X<int>::xxx(x); // OK.
    X<int&>::xxx(x); // ERROR!
    return 0;
}

Error message (GCC):

error: ‘static void X::xxx(T&&) [with T = int&]’ cannot be overloaded
error: with ‘static void X::xxx(T&) [with T = int&]’

Why? T = int& —> Is T& replaced by int&& in static void xxx(T& x)?

If the answer to the question is yes, then:

  • T& is not a lvalue-reference and it becomes a rvalue-reference!
  • And the following code should work:

But it didn’t:

template<class T> struct X
{
    static void xxx(T& x) { }
};

int main(int argc, char** argv)
{
    X<int&>::xxx(2); // ERROR!
    return 0;
}

Error Message (GCC):

error: no matching function for call to ‘X::xxx(int)’
note: candidates are: static void X::xxx(T&) [with T = int&]

Then T& with T = int& is not equal to T&& and is not a rvalue-reference. but if it is not, why the first example not working? (it’s a recursive problem!)


But the similar problem didn’t occur for pointer types:

#include <iostream>

template<class T> struct X
{
    static void xxx(T* x) { std::cout << **x << std::endl; }
};

int main(int argc, char** argv)
{
    int x = 10;
    int* xx = &x;
    X<int*>::xxx(&xx); // OK. call X<int*>::xxx(int**)
    return 0;
}

Why references are different in this behavior?

  • 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-16T11:26:25+00:00Added an answer on May 16, 2026 at 11:26 am

    The C++11 language standard has an explanation of how this works at §8.3.2[dcl.ref]/6 (reformatted for readability):

    If a typedef, a type template-parameter, or a decltype-specifier denotes a type TR that is a reference to a type T,

    • an attempt to create the type "lvalue reference to cv TR" creates the type "lvalue reference to T"
    • an attempt to create the type "rvalue reference to cv TR" creates the type TR.

    Let’s consider your example (I’ve renamed your T to be TR so it matches the language above):

    template<class TR> struct X
    {
        static void xxx(TR& x)  { }
        static void xxx(TR&& x) { }
    };
    

    If we try instantiating X with TR = int& (so, T = int), the instantiations of xxx are as follows:

    static void xxx(TR& x)  { }   -->   static void xxx(int& x) { }
    static void xxx(TR&& x) { }   -->   static void xxx(int& x) { }
    

    In the first case, we attempt to create an "lvalue reference to TR," which becomes an "lvalue reference to T." T is int, so the parameter type becomes int&.

    In the second case, we attempt to create an "rvalue reference to TR," which becomes TR, which is int&.

    The parameter type is the same for both overloads, hence the error.

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

Sidebar

Related Questions

I have big dataset (but the following is small one for example). I can
I am testing a small stub of Matcher and Pattern class...see the following small
I have a small I have just written which uses the following uri template.
I am implementing a small database(university Project) and i am facing the following problem.
I encounter the following small annoying dilemma over and over again in Python: Option
I have following small script to preview some text before submitting it to store
A small - little question. I have seen many application having buttons like following.
I have the following parser grammar (this is a small sample): expr: ident assignop
I'm doing a small app in Excel, and I have the following code when
Silverlight newbie question: I've got a small Silverlight application with the following XAML snippet:

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.