std::is_const
Min standard notice:
Header: <type_traits>
std::is_const is a UnaryTypeTrait.
# Declarations
template< class T >
struct is_const;
(since C++11)
# Notes
If T is a reference type then is_const
# Example
#include <type_traits>
static_assert(std::is_same_v<const int*, int const*>,
"Remember, constness binds tightly inside pointers.");
static_assert(!std::is_const_v<int>);
static_assert(std::is_const_v<const int>);
static_assert(!std::is_const_v<int*>);
static_assert(std::is_const_v<int* const>,
"Because the pointer itself can't be changed but the int pointed at can.");
static_assert(!std::is_const_v<const int*>,
"Because the pointer itself can be changed but not the int pointed at.");
static_assert(!std::is_const_v<const int&>);
static_assert(std::is_const_v<std::remove_reference_t<const int&>>);
struct S
{
void foo() const {}
void bar() const {}
};
int main()
{
// A const member function is const in a different way:
static_assert(!std::is_const_v<decltype(&S::foo)>,
"Because &S::foo is a pointer.");
using S_mem_fun_ptr = void(S::*)() const;
S_mem_fun_ptr sfp = &S::foo;
sfp = &S::bar; // OK, can be re-pointed
static_assert(!std::is_const_v<decltype(sfp)>,
"Because sfp is the same pointer type and thus can be re-pointed.");
const S_mem_fun_ptr csfp = &S::foo;
// csfp = &S::bar; // Error
static_assert(std::is_const_v<decltype(csfp)>,
"Because csfp cannot be re-pointed.");
}