Namespaces
Variants

std:: is_const

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
is_const
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
헤더 파일에 정의됨 <type_traits>
template < class T >
struct is_const ;
(C++11부터)

std::is_const UnaryTypeTrait 입니다.

만약 T 가 const 한정 타입(즉, const , 또는 const volatile )인 경우, 멤버 상수 value true 로 제공합니다. 다른 모든 타입에 대해서는 value false 입니다.

프로그램이 std::is_const 또는 std::is_const_v 에 대한 특수화를 추가하는 경우, 그 동작은 정의되지 않습니다.

목차

템플릿 매개변수

T - 확인할 타입

헬퍼 변수 템플릿

template < class T >
constexpr bool is_const_v = is_const < T > :: value ;
(C++17부터)

std:: integral_constant 로부터 상속됨

멤버 상수

value
[static]
true 만약 T 가 const 한정 타입이면, false 그렇지 않으면
(public static member constant)

멤버 함수

operator bool
객체를 bool 로 변환, value 반환
(public member function)
operator()
(C++14)
value 반환
(public member function)

멤버 타입

타입 정의
value_type bool
type std:: integral_constant < bool , value >

참고 사항

만약 T 가 참조 타입이라면 is_const < T > :: value 는 항상 false 입니다. 참조 타입이 될 수 있는 타입의 constness를 확인하는 올바른 방법은 참조를 제거하는 것입니다: is_const < typename remove_reference < T > :: type > .

가능한 구현

template<class T> struct is_const          : std::false_type {};
template<class T> struct is_const<const T> : std::true_type {};

예제

#include <type_traits>
static_assert(std::is_same_v<const int*, int const*>,
    "기억하세요, const는 포인터 내에서 강하게 결합합니다.");
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>,
    "포인터 자체는 변경할 수 없지만 가리키는 int는 변경할 수 있기 때문입니다.");
static_assert(!std::is_const_v<const int*>,
    "포인터 자체는 변경할 수 있지만 가리키는 int는 변경할 수 없기 때문입니다.");
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()
{
    // const 멤버 함수는 다른 방식으로 const입니다:
    static_assert(!std::is_const_v<decltype(&S::foo)>,
        "&S::foo는 포인터이기 때문입니다.");
    using S_mem_fun_ptr = void(S::*)() const;
    S_mem_fun_ptr sfp = &S::foo;
    sfp = &S::bar; // OK, 재지정 가능
    static_assert(!std::is_const_v<decltype(sfp)>,
        "sfp는 동일한 포인터 타입이므로 재지정할 수 있기 때문입니다.");
    const S_mem_fun_ptr csfp = &S::foo;
    // csfp = &S::bar; // 오류
    static_assert(std::is_const_v<decltype(csfp)>,
        "csfp는 재지정할 수 없기 때문입니다.");
}

참고 항목

타입이 volatile로 한정되었는지 검사합니다
(클래스 템플릿)
(C++17)
인자에 대한 const 참조를 얻습니다
(함수 템플릿)