types_is_default_constructible

types_is_default_constructible (기본 생성 가능성 검사)

이 페이지에서는 C++ 표준 라이브러리의 타입 특성(type trait) 템플릿인 is_default_constructible, is_trivially_default_constructible, is_nothrow_default_constructible에 대해 설명해요. 이 템플릿들은 주어진 타입이 기본 생성 가능한지, 사소하게(trivially) 기본 생성 가능한지, 예외 없이(nothrow) 기본 생성 가능한지를 컴파일 타임에 판별해요. 조건을 만족하면 true를, 그렇지 않으면 false를 값으로 가지는 std::integral_constant를 상속받아요.

출처: cppreference

본문

헤더 <type_traits>에 정의됨
template < class T > struct is_default_constructible ; (1) (since C++11)
template < class T > struct is_trivially_default_constructible ; (2) (since C++11)
template < class T > struct is_nothrow_default_constructible ; (3) (since C++11)

T가 완전한 타입(complete type)이 아니거나, (cv 한정될 수 있는) void이거나, 경계를 알 수 없는 배열(array of unknown bound)이라면, 동작은 정의되지 않아요.

위 템플릿의 인스턴스화가 직접적으로든 간접적으로든 불완전한 타입에 의존하고, 그 타입이 가상으로 완성되었을 때 인스턴스화 결과가 달라질 수 있다면, 동작은 정의되지 않아요.

프로그램이 이 페이지에 설명된 템플릿 중 하나에 대해 특수화를 추가하면, 동작은 정의되지 않아요.

헬퍼 변수 템플릿

template < class T > constexpr bool is_default_constructible_v = is_default_constructible < T >:: value ; (since C++17)
template < class T > constexpr bool is_trivially_default_constructible_v = is_trivially_default_constructible < T >:: value ; (since C++17)
template < class T > constexpr bool is_nothrow_default_constructible_v = is_nothrow_default_constructible < T >:: value ; (since C++17)

std:: integral_constant에서 상속됨

멤버 상수

value [static] T가 기본 생성 가능하면 true, 그렇지 않으면 false (공용 정적 멤버 상수)

멤버 함수

operator bool 객체를 bool로 변환하고, value를 반환해요 (공용 멤버 함수)
operator() (C++14) value를 반환해요 (공용 멤버 함수)

멤버 타입

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

가능한 구현

template < class T > struct is_default_constructible : std :: is_constructible < T > {}; template < class T > struct is_trivially_default_constructible : std :: is_trivially_constructible < T > {}; template < class T > struct is_nothrow_default_constructible : std :: is_nothrow_constructible < T > {};

참고 사항

많은 구현에서 std::is_nothrow_default_constructible은 소멸자가 예외를 던지는지도 확인해요. 왜냐하면 그것이 실질적으로 noexcept(T())이기 때문이에요. std::is_trivially_default_constructible에도 같은 내용이 적용되는데, 이 구현들에서는 소멸자가 사소한(trivial) 것도 요구해요: GCC 버그 51452, LWG 이슈 2116.

std :: is_default_constructible < T >는 T x;가 컴파일되는지를 검사하지 않아요. 대신 빈 인자 목록으로 직접 초기화(direct-initialization)를 시도해요 (std::is_constructible 참고). 따라서 std :: is_default_constructible_v < const int >와 std :: is_default_constructible_v < const int [ 10 ] >는 true예요.

예제

#include <string>
#include <type_traits>

struct S1
{
    std::string str; // member has a non-trivial default constructor
};
static_assert(std::is_default_constructible_v<S1> == true);
static_assert(std::is_trivially_default_constructible_v<S1> == false);

struct S2
{
    int n;
    S2() = default; // trivial and non-throwing
};
static_assert(std::is_trivially_default_constructible_v<S2> == true);
static_assert(std::is_nothrow_default_constructible_v<S2> == true);

int main() {}

같이 보기

is_constructible is_trivially_constructible is_nothrow_constructible (C++11) (C++11) (C++11) 타입이 특정 인자에 대한 생성자를 가지고 있는지 확인해요 (클래스 템플릿) [edit]
is_copy_constructible is_trivially_copy_constructible is_nothrow_copy_constructible (C++11) (C++11) (C++11) 타입이 복사 생성자를 가지고 있는지 확인해요 (클래스 템플릿) [edit]
is_move_constructible is_trivially_move_constructible is_nothrow_move_constructible (C++11) (C++11) (C++11) 타입이 rvalue 참조로부터 생성될 수 있는지 확인해요 (클래스 템플릿) [edit]
default_initializable (C++20) 타입의 객체가 기본 생성될 수 있음을 명시해요 (컨셉) [edit]
is_default_constructible_type is_trivially_default_constructible_type is_nothrow_default_constructible_type (C++26) (C++26) (C++26) 반영된 타입이 기본 생성자를 가지고 있는지 확인해요 (함수) [edit]

더 알아보기 (Learn more)

cppreference