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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:28:12+00:00 2026-06-12T10:28:12+00:00

It appears that to test for const-ness, one must test the template-parameter, but to

  • 0

It appears that to test for const-ness, one must test the template-parameter, but to test for rvalue-ness, one must test an actual parameter. (This is using VC++ 2012.) This code illustrates what I mean:

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

using namespace std;

template<class T>
void f(T& x) {
    cout << "f() is_const<T> and is_const<decltype<x)>" << endl;
    cout << is_const<T>::value << endl; // Prints 1 when arg is const
    cout << is_const<decltype(x)>::value << endl; // Prints 0 when arg is const
}

template<class T>
void g(T&& x) {
    cout << "g() is_const<T> and is_const<decltype<x)>" << endl;
    cout << is_const<T>::value << endl; // Prints 0 when arg is const
    cout << is_const<decltype(x)>::value << endl; // Prints 0 when arg is cons
    cout << "g() is_rvalue_reference<T> and is_rvalue_reverence<decltype(x)>" <<endl;
    cout << is_rvalue_reference<T>::value << endl; // Prints 0 when arg is rvlaue
    cout << is_rvalue_reference<decltype(x)>::value << endl; // Prints 1 when arg is rvalue
}

int main()
{
    const std::string str;
    f(str); // const argument
    cout << endl;
    g(std::string("")); // rvalue argument
    return 0;
} 

I am struggling to understand why that is. Can someone explain, or point me to an article that explains it? If need be, I will dig into the C++11 standard. Anyone know the pertinent sections?

  • 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-12T10:28:14+00:00Added an answer on June 12, 2026 at 10:28 am

    The reason is that you’re misunderstanding things. x will never be const in any of those examples, simply because there are no const reference types (you can’t change what a reference refers to anyways). In is_const<T> you’re basically ignoring that you declared x as T&.

    A similar misunderstanding is at work for the rvalue ref test. The T in T&& (which is called a universal reference, btw) will be deduced as U& when you pass an lvalue and as U when you pass an rvalue. When testing is_rvalue_reference<T>, you’re ignoring again that you declared x as T&&. When testing is_const<T>, you didn’t account for the fact that T will be a reference, which, as said above, can never be const.

    The correct tests for g would be

    • std::is_const<typename std::remove_reference<T>::type>::value and
    • std::is_rvalue_reference<T&&>::value
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It appears that we will have to build/deploy one of our new JBoss apps
I am using CPPUnit to test a class in my program. This class (
This is not important. But I'm curious as to when this warning appears. My
I'm running a recorded load test in JMeter. It appears that when the test
Appears that WAS does not support the integration binding. I've tried setting it up
It appears that when I do git stash apply, I need to then type
It appears that second windows are not closing. Here is what I am doing....
It appears that add_to_class() https://github.com/django/django/blob/master/django/db/models/base.py#L218 doesn't actually add a column, but adds a field
It appears that a lot of software, including low-level system calls, etc., relies on
It appears that NetBeans and Aptana/RadRails are the most common adopted IDE's for Ruby

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.