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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:26:21+00:00 2026-06-14T02:26:21+00:00

Can anyone help tell me why the argument deduction is not working how I

  • 0

Can anyone help tell me why the argument deduction is not working how I would anticipate? Please see my code comments for my line of thinking?

#include <iostream>
#include <type_traits>
#include <iomanip>
#include <string>

using namespace std;

template<class T>
void deduce1(T args, string arg){
cout << "template<class T> void deduce1(T args) " << " argument passed in was: " << arg << " deduced as: " << typeid(T).name() << endl;
cout <<  "Is const: " << boolalpha << is_const<T>::value << endl;
cout <<  "Is reference: " << boolalpha << is_reference<T>::value << endl;
cout <<  "Is pointer: " << boolalpha << is_pointer<T>::value << endl;
}

template<class T>
void deduce2(T& args,string arg){
cout << "template<class T> void deduce2(T args) " << " argument passed in was: " << arg << " deduced as: " << typeid(T).name() << endl;
cout <<  "Is const: " << boolalpha << is_const<T>::value << endl;
cout <<  "Is reference: " << boolalpha << is_reference<T>::value << endl;
cout <<  "Is pointer: " << boolalpha << is_pointer<T>::value << endl;
}

template<class T>
void deduce3(T&& args,string arg){
cout << "template<class T> void deduce3(T args) " << " argument passed in was: " << arg << " deduced as: " << typeid(T).name() << endl;
cout <<  "Is const: " << boolalpha << is_const<T>::value << endl;
cout <<  "Is reference: " << boolalpha << is_reference<T>::value << endl;
cout <<  "Is rvalue reference: " << boolalpha << is_rvalue_reference<T>::value << endl;
cout <<  "Is pointer: " << boolalpha << is_pointer<T>::value << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{

int a = 1;
const int b = 5;
int c[] = {12};
int const d[] = {12};
int& e = a;

deduce1(a,"int a = 1");
deduce1(b,"const int b = 5");
deduce1(c,"int c[] = {12}");
deduce1(d,"int const d[] = {12}"); // would have thought is_const<T> would return true any comments?
deduce1(e,"int& e = a");
deduce1(5,"5");

deduce2(a,"int a = 1");
deduce2(b,"const int b = 5"); //would have though type would be deduced as int const comments?
deduce2(c,"int c[] = {12}"); // why is this not returning true as a reference?
deduce2(d,"int const d[] = {12}"); // would have thought is_const<T> would return true any comments
deduce2(e,"int& e = a");

deduce3(a,"int a = 1");
deduce3(b,"const int b = 5");
deduce3(c,"int c[] = {12}"); // why is this not returning true as a reference?
deduce3(d,"int const d[] = {12}"); // would have thought is_const<T> would return true any comments
deduce3(e,"int& e = a");
deduce3(string("Hello"),"string(\"Hello\")"); // why not rvalue reference


return 0;
}
  • 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-14T02:26:22+00:00Added an answer on June 14, 2026 at 2:26 am

    I’ll tackle each of your comments in turn.

    1. deduce1 with d:

      template <class T> void deduce1(T args, string arg);
      int const d[] = {12};
      deduce1(d, "int const d[] = {12}"); // would have thought is_const<T> would return true
      

      The first important thing is that you can’t pass arrays as arguments by value in C++. Expressions denoting an array will decay almost immediately to a pointer to its first element. So the type deduced for T here will be int const *.

      So still, why won’t this work? The problem is that the pointer is not const. The ints that the pointer is pointing to are const. If you want to report the constness of the type being pointed to, you’ll need to use remove_pointer before checking the constness. So you would change your const checking to:

      cout <<  "Is const: " << boolalpha << is_const<typename remove_pointer<T>::type>::value << endl;
      

      However, the result it was giving before was the correct answer. The pointer was indeed not const. In fact, even if the pointer were const, top-level consts are stripped off before type deduction is done so T would not be const anyway. The reason for this is that, if you’re passing by value, you really don’t care about the constness of the argument since you’re copying it anyway.

    2. deduce2 with b:

      template <class T> void deduce2(T& args,string arg);
      const int b = 5;
      deduce2(b,"const int b = 5"); //would have though type would be deduced as int const
      

      Not sure what’s happening for you here, but my output is:

      deduced as: i
      Is const: true
      Is reference: false
      Is pointer: false
      

      Exactly as expected.

    3. deduce2 with c:

      int c[] = {12};
      deduce2(c,"int c[] = {12}"); // why is this not returning true as a reference?
      

      It’s saying that it’s not a reference for the same reason all of the deduce2 calls say that (and you have this problem throughout deduce3 too). In your function, you’re checking the type of T but the type of args is T&. You’re only checking the part before the &. So here, the type of args is int (&)[1] c but you’re just checking if int [1] is a reference. Fix it by using decltype(args) instead of T in your checks.

    4. deduce2 with d:

      int const d[] = {12};
      deduce2(d,"int const d[] = {12}"); // would have thought is_const<T> would return true
      

      Same problem as deduce1 with d (item 1).

    5. deduce3 with c:

      template <class T> void deduce3(T&& args,string arg);
      int c[] = {12};
      deduce3(c,"int c[] = {12}"); // why is this not returning true as a reference?
      

      Same problem as deduce2 with c (item 3).

    6. deduce3 with d:

      int const d[] = {12};
      deduce3(d,"int const d[] = {12}"); // would have thought is_const<T> would return true
      

      Same problem as deduce1 with d (item 1).

    7. deduce3 with string("Hello"):

      deduce3(string("Hello"),"string(\"Hello\")"); // why not rvalue reference
      

      I’m going to assume you know something about “universal references”. Scott Meyers has a great article and talk on the subject.

      In this case, because string("Hello") is an rvalue expression, the type T is deduced as string (by the rules of deduced rvalue reference types of the form T&&). The type of args is now string &&. So if you check if decltype(args) is an rvalue, it’ll say true. But T itself is not.

    If you fix your code so that all type traits on T are now on decltype(args), you’ll get a new problem. When args is of reference type, certain types that you expect to be const won’t be. That’s because there’s no such thing as a const reference. Similar to using remove_pointer in item 1, you’ll need to use remove_reference to get at the underlying type and check if that is const.

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

Sidebar

Related Questions

Can anyone help me with this code and tell me what is wrong? My
Can anyone tell me where com.ibm.xsp.component.UIIncludeComposite is documented? I searched in the help file
Can anyone help me find an up-to-date, working ATL project which has a main
Can anyone tell help me understand the new CRUD scaffolding that is included with
Can anyone please tell me how to write NSMutableArray of custom class objects to
Can anyone please tell me how to login? I am trying to login in
Can anyone help me please? I'm using VS2010, C++ Custom Action Project. Trying to
Can anyone help me debug this program? The following is server code : package
I'm not very good with P/Invoke. Can anyone tell me how to declare and
i have not found a suitable answer can anyone plese tell me the way

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.