types_is_constructible

types_is_constructible (타입 생성 가능성 판별)

이 페이지에서는 C++ 표준 라이브러리의 is_constructible 및 관련 타입 특성에 대해 설명해요. 주어진 타입 T가 인자 목록 Args...로부터 생성 가능한지, 그리고 그 생성이 trivial 또는 noexcept인지 컴파일 타임에 판별할 수 있어요. 이 특성들은 템플릿 메타프로그래밍에서 오버로드 선택이나 제약 조건을 구현할 때 유용하게 사용돼요.

출처: cppreference

본문

<type_traits> 헤더에 정의되어 있어요.

<type_traits> 헤더에 정의됨
template < class T , class ... Args > struct is_constructible ; (1) (since C++11)
template < class T , class ... Args > struct is_trivially_constructible ; (2) (since C++11)
template < class T , class ... Args > struct is_nothrow_constructible ; (3) (since C++11)

만약 T 또는 인자 팩 Args 안의 어떤 타입이 완전한 타입(complete type)이 아니거나, (cv 한정된) void이거나, 경계를 알 수 없는 배열이라면, 동작은 정의되지 않아요.

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

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

헬퍼 변수 템플릿

template < class T , class ... Args > constexpr bool is_constructible_v = is_constructible < T , Args ... >:: value ; (since C++17)
template < class T , class ... Args > constexpr bool is_trivially_constructible_v = is_trivially_constructible < T , Args ... >:: value ; (since C++17)
template < class T , class ... Args > constexpr bool is_nothrow_constructible_v = is_nothrow_constructible < T , Args ... >:: value ; (since C++17)

std::integral_constant에서 상속됨

멤버 상수

value [static] TArgs...로부터 생성 가능하면 true, 아니면 false (공용 정적 멤버 상수)

멤버 함수

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

멤버 타입

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

참고 사항

많은 구현에서 is_nothrow_constructiblenoexcept(T(arg))가 실질적으로 유효한지 확인하기 위해 소멸자가 예외를 던지는지도 검사해요. is_trivially_constructible에도 동일하게 적용되며, 이러한 구현에서는 소멸자가 trivial해야 한다는 요구도 추가돼요. (GCC 버그 51452, LWG 이슈 2116)

예제

#include <iostream>
#include <type_traits>

class Foo
{
    int v1;
    double v2;
public:
    Foo(int n) : v1(n), v2() {}
    Foo(int n, double f) noexcept : v1(n), v2(f) {}
};

int main()
{
    auto is = [](bool o) { return (o ? "\t" "is " : "\t" "isn't "); };
    std::cout << "Foo ...\n"
              << is(std::is_trivially_constructible_v<Foo, const Foo&>)
              << "Trivially-constructible from const Foo&\n"
              << is(std::is_trivially_constructible_v<Foo, int>)
              << "Trivially-constructible from int\n"
              << is(std::is_constructible_v<Foo, int>)
              << "Constructible from int\n"
              << is(std::is_nothrow_constructible_v<Foo, int>)
              << "Nothrow-constructible from int\n"
              << is(std::is_nothrow_constructible_v<Foo, int, double>)
              << "Nothrow-constructible from int and double\n";
}

출력:

Foo ...
        is Trivially-constructible from const Foo&
        isn't Trivially-constructible from int
        is Constructible from int
        isn't Nothrow-constructible from int
        is Nothrow-constructible from int and double

같이 보기

is_default_constructible is_trivially_default_constructible is_nothrow_default_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]
constructible_from (C++20) 타입의 변수가 인자 타입 집합으로부터 생성되거나 바인딩될 수 있음을 명시해요 (콘셉트) [edit]
is_constructible_type is_trivially_constructible_type is_nothrow_constructible_type (C++26) (C++26) (C++26) 반영된 타입이 특정 인자에 대한 생성자를 가지고 있는지 검사해요 (함수 템플릿) [edit]

더 알아보기 (Learn more)

cppreference