How do I check if an object is const without C++11’s std::is_const? As far as I know I shouldn’t be const_casting an object that was declared const
How do I check if an object is const without C++11’s std::is_const ? As
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
An example implementation for C++11’s
is_constis given on cppreference, and it looks like this:If you put this definition in your C++03 code, you can use
is_constthere as well, if you add definitions forfalse_typeandtrue_type(thanks to mfonantini for pointing out the missingtrue_typeandfalse_type). If you define them as follows, you’ll get very close to the definition used in C++11:The only difference is that the static
valueis a mereconst, not aconstexpr, but note that it is a constant expression nevertheless and can be used as template argument. So for all practical purposes, the definition above should work in C++03.Regarding the last part of your question: There is actually no problem casting a non-const type to const. (Illegal situations can, however, arise with pointers to pointers or references to pointers, e.g.
T**cannot be cast toconst T**.)