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

  • Home
  • SEARCH
  • 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 9017213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:12:32+00:00 2026-06-16T04:12:32+00:00

Is it possible to convert the is_const expressions into a test function or is

  • 0

Is it possible to convert the is_const expressions into a test function or is this impossible because top level cv-qualifieres are ignored during template type deduction?

int main()
{
  using std::is_const;

  const int x = 0;
  int y = 0;

  // move to "bool test()"
  std::cout
    << "main, x: " << is_const<decltype(x)>::value << "\n"  // true
    << "main, y: " << is_const<decltype(y)>::value << "\n"  // false
    ;

  std::cout
    << "test, x: " << test(x) << "\n"  // false, I wanted true
    << "test, y: " << test(y) << "\n"  // false
    ;
}

I have unsuccessfully tried various versions similar to:

template<typename T>
bool test(T x)
{
  return is_const<???>::value;
}

I want to make sure that I am not missing something and that writing such a testfunction is indeed impossible. (If it was possible, I would also like to know whether a C++03 version was possible.)

Thank you for your consideration

Update

Due to Mankarse I learned that type deduction is different in case of rvalue references:

template<typename T> void t1(T x);
template<typename T> void t2(T& x);
template<typename T> void t3(T&& x);

const int x = 42;
int y = 0;

t1(x);  // T = int:        t1<int>(int x)
t1(y);  // T = int:        t1<int>(int x)

t2(x);  // T = const int: t2<const int>(const int& x)
t2(y);  // T = int: t2<int>(int& x)

t3(x);  // T = const int&: t3<const int&>(const int& && x)
t3(y);  // T = int&:       t3<int&>(int& && x)
  • 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-16T04:12:33+00:00Added an answer on June 16, 2026 at 4:12 am

    In C++11, this can be done with perfect forwarding rvalue references:

    template<typename T>
    bool test(T&& x)
    {
        return std::is_const<typename std::remove_reference<T>::type>::value;
    }
    

    In C++03, you can instead use an lvalue reference:

    template<typename T>
    bool test(T& x) {
        return boost::is_const<T>::value;
    }
    

    The differences between the two are demonstrated below:

    typedef int const intc;
    intc x = intc();
    int y = int();
    std::cout                                     // C++11 C++03
      << "x: " << test(x) << "\n"                 // 1     1
      << "y: " << test(y) << "\n"                 // 0     0
      << "move(x): " << test(std::move(x)) << "\n"// 1     1 (when compiled as C++11)
      << "move(y): " << test(std::move(y)) << "\n"// 0     compilation error
      << "intc{}: " << test(intc()) << "\n"       // 0     compilation error
      << "int{}: " << test(int()) << "\n"         // 0     compilation error
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to convert this java code snippet to php? public void testBalanceReturnsToZeroOnVending()
Is it possible to convert HTML + CSS into HTML for a system that
Is it possible to convert facebook data (ex. user_pic) into a DisplayObject so I
It is possible to convert a varchar(32) (a hexadecimal string like 0x81f2cf442269e111b99c1cc1deedf59c) to a
Is it possible to convert the font of a matlab plot to be the
Is it possible to convert a FF Jetpack extension to a chrome's one? I've
Is it possible to convert a String to a type, which is definied by
Is it possible to convert some of the dlls of .Net framework 3.5 and
Is it possible to convert a GroupCollection to a List or an IEnumerable ?
Is it possible to convert Anim8or to Cinema 4D?

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.