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

The Archive Base Latest Questions

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

Consider this code: #include <iostream> #include <type_traits> using namespace std; template<typename T_orig> void f(T_orig&

  • 0

Consider this code:

#include <iostream>
#include <type_traits>
using namespace std;

template<typename T_orig> void f(T_orig& a){
    a=5;
}


template<typename T_orig, typename T=T_orig&> void g(T a){
    a=8;
}

int main() {
    int b=3;
    f<decltype(b)>(b);
    cout<<b<<endl;
    g<decltype(b)>(b);
    cout<<b<<endl;
    return 0;
}

This prints

5
5

Can somebody explain to me why in the second version the & is lost?

  • 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-15T01:18:47+00:00Added an answer on June 15, 2026 at 1:18 am

    The problem here is that type deduction takes priority over defaulted function template parameters. Therefore you get the T parameter deduced and T never deduces to a reference.

    You can prevent this by making the type not deducible. A generic identity type trait can do this.

    template <typename T>
    struct identity { using type = T; };
    
    template <typename T>
    using NotDeducible = typename identity<T>::type;
    
    template<typename T_orig, typename T=typename target<T_orig>::T>
    void g(NotDeducible<T> a) { // blah
    

    Or, in this particular case, you can simply get rid of the template parameter altogether.

    template<typename T_orig> void g(typename target<T_orig>::T a)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code : #include <iostream> #include <typeinfo> using namespace std; template<typename T1, typename
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
Consider this code: #include <iostream> using namespace std; typedef int array[12]; array sample; array
Please consider this code: #include <iostream> template<typename T> void f(T x) { std::cout <<
Consider this code: #include <iostream> void f(int&& i) { std::cout << f(int&&)\n; } void
Consider this piece of code: #include <iostream> #include <vector> template<typename A> void foo(A& a)
Consider this C++ code: #include <iostream> using namespace std; struct B { virtual int
Consider this: #include <iostream> using namespace std; class A{ protected: void some_function(int params) {
Consider the following code: #include <iostream> #include <functional> using namespace std; template<class T> void
Consider this code: #include <iostream> using namespace std; int main() { bool lock =

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.