Namespaces
Variants

std:: is_constructible, std:: is_trivially_constructible, std:: is_nothrow_constructible

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(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, class ... Args >
struct is_constructible ;
(1) (C++11부터)
template < class T, class ... Args >
struct is_trivially_constructible ;
(2) (C++11부터)
template < class T, class ... Args >
struct is_nothrow_constructible ;
(3) (C++11부터)
1) 만약 T 가 객체 또는 참조 타입이고 변수 정의 T obj ( std:: declval < Args > ( ) ... ) ; 가 유효한 형식이라면, 멤버 상수 value true 로 제공합니다. 다른 모든 경우에는 value false 입니다.
이 검사의 목적상, 변수 정의는 함수 선언으로 해석되지 않으며 std::declval 의 사용은 ODR-사용 으로 간주되지 않습니다. 접근 검사 T Args 내의 어떤 타입과도 무관한 컨텍스트에서 수행된 것처럼 처리됩니다. 변수 정의의 직접적인 컨텍스트 유효성만 고려됩니다.
2) (1) 과 동일하지만, 변수 정의에서 trivial하지 않은 연산을 호출하지 않습니다. 이 검사의 목적상, std::declval 호출은 trivial한 것으로 간주됩니다.
3) (1) 과 동일하지만, 변수 정의가 noexcept 입니다.

만약 T 또는 매개변수 팩 Args 내의 어떤 타입이 완전한 타입이 아니거나, (cv-qualified일 수 있는) void , 또는 알려지지 않은 경계의 배열인 경우, 그 동작은 정의되지 않습니다.

템플릿의 인스턴스화가 직접적 또는 간접적으로 불완전한 타입에 의존하고, 해당 타입이 가상적으로 완성되었을 때 다른 결과를 초래할 수 있는 경우, 그 동작은 정의되지 않습니다.

프로그램이 이 페이지에 설명된 템플릿들 중 어느 하나에 대해 특수화를 추가하는 경우, 동작은 정의되지 않습니다.

목차

헬퍼 변수 템플릿

template < class T, class ... Args >

inline constexpr bool is_constructible_v =

is_constructible < T, Args... > :: value ;
(C++17부터)
template < class T, class ... Args >

inline constexpr bool is_trivially_constructible_v =

is_trivially_constructible < T, Args... > :: value ;
(C++17부터)
template < class T, class ... Args >

inline constexpr bool is_nothrow_constructible_v =

is_nothrow_constructible < T, Args... > :: value ;
(C++17부터)

std:: integral_constant 로부터 상속됨

멤버 상수

value
[static]
true 만약 T Args... 로부터 생성 가능한 경우, 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 >

참고 사항

많은 구현에서, is_nothrow_constructible 는 소멸자가 예외를 던지는지 여부도 확인합니다. 이는 실제로 noexcept ( T ( arg ) ) 와 동일하기 때문입니다. 동일한 사항이 is_trivially_constructible 에도 적용되며, 이러한 구현에서는 소멸자가 trivial해야 합니다: GCC bug 51452 LWG issue 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

참고 항목

타입이 기본 생성자를 가지는지 검사합니다
(클래스 템플릿)
타입이 복사 생성자를 가지는지 검사합니다
(클래스 템플릿)
타입이 우측값 참조로부터 생성될 수 있는지 검사합니다
(클래스 템플릿)
해당 타입의 변수가 일련의 인자 타입들로부터 생성되거나 바인딩될 수 있음을 명시합니다
(컨셉)