types_is_copy_constructible
types_is_copy_constructible (복사 생성 가능 여부를 확인하는 타입 특성)
이 페이지는 C++ 표준 라이브러리 <type_traits>에서 제공하는 타입 특성(type trait) 템플릿들을 설명해요. is_copy_constructible, is_trivially_copy_constructible, is_nothrow_copy_constructible을 통해 주어진 타입이 복사 생성 가능한지, 그리고 그 복사 생성자가 사소한지 또는 예외를 던지지 않는지를 컴파일 타임에 확인할 수 있어요. 이 특성들은 템플릿 메타프로그래밍이나 static_assert와 함께 조건부 코드를 작성할 때 유용하게 사용돼요.
출처: cppreference
본문
템플릿 선언
<type_traits> 헤더에 정의됨 |
||
|---|---|---|
template < class T > struct is_copy_constructible ; |
(1) | (since C++11) |
template < class T > struct is_trivially_copy_constructible ; |
(2) | (since C++11) |
template < class T > struct is_nothrow_copy_constructible ; |
(3) | (since C++11) |
T가 완전한 타입(complete type)이 아니거나, (cv 한정된) void이거나, 크기를 알 수 없는 배열이라면 동작이 정의되지 않아요.
위 템플릿의 인스턴스화가 직접 또는 간접적으로 불완전한 타입에 의존하고, 그 타입이 가상으로 완성되었을 때 인스턴스화 결과가 달라질 수 있다면 동작이 정의되지 않아요.
프로그램이 이 페이지에 설명된 템플릿에 대해 특수화를 추가하면 동작이 정의되지 않아요.
헬퍼 변수 템플릿
| 템플릿 | ||
|---|---|---|
template < class T > constexpr bool is_copy_constructible_v = is_copy_constructible < T >:: value ; |
(since C++17) | |
template < class T > constexpr bool is_trivially_copy_constructible_v = is_trivially_copy_constructible < T >:: value ; |
(since C++17) | |
template < class T > constexpr bool is_nothrow_copy_constructible_v = is_nothrow_copy_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_copy_constructible : std :: is_constructible < T , typename std :: add_lvalue_reference < typename std :: add_const < T >:: type >:: type > {}; template < class T > struct is_trivially_copy_constructible : std :: is_trivially_constructible < T , typename std :: add_lvalue_reference < typename std :: add_const < T >:: type >:: type > {}; template < class T > struct is_nothrow_copy_constructible : std :: is_nothrow_constructible < T , typename std :: add_lvalue_reference < typename std :: add_const < T >:: type >:: type > {};
참고 사항
많은 구현에서 is_nothrow_copy_constructible은 실제로 noexcept(T(arg))처럼 동작하기 때문에 소멸자가 예외를 던지는지도 함께 확인해요. is_trivially_copy_constructible도 마찬가지로, 이러한 구현에서는 소멸자가 사소한지도 요구해요. (GCC bug 51452, LWG issue 2116)
std::is_copy_constructible_v는 복사 생성자가 존재하지만 호출 자체가 ill-formed인 타입, 예를 들어 std :: vector < std :: mutex > 같은 타입에 대해서도 true를 보고할 수 있어요.
예제
#include <string>
#include <type_traits>
struct S1
{
std::string str; // member has a non-trivial copy constructor
};
static_assert(std::is_copy_constructible_v<S1>);
static_assert(!std::is_trivially_copy_constructible_v<S1>);
struct S2
{
int n;
S2(const S2&) = default; // trivial and non-throwing
};
static_assert(std::is_trivially_copy_constructible_v<S2>);
static_assert(std::is_nothrow_copy_constructible_v<S2>);
struct S3
{
S3(const S3&) = delete; // explicitly deleted
};
static_assert(!std::is_copy_constructible_v<S3>);
struct S4
{
S4(S4&) {}; // cannot bind const, hence not a copy-constructible
};
static_assert(!std::is_copy_constructible_v<S4>);
int main() {}
결함 보고
다음의 동작 변경 결함 보고는 이전에 발표된 C++ 표준에 소급 적용되었어요.
| DR | 적용 대상 | 이전 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2196 | C++11 | const T&를 형성할 수 없을 때 동작이 불명확했어요 |
이 경우 생성되는 값이 false가 되도록 수정되었어요 |
같이 보기
is_constructible is_trivially_constructible is_nothrow_constructible (C++11) (C++11) (C++11) |
특정 인자에 대해 타입에 생성자가 있는지 확인해요 (클래스 템플릿) [edit] |
|---|---|
is_default_constructible is_trivially_default_constructible is_nothrow_default_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] |
copy_constructible (C++20) |
타입의 객체가 복사 생성 및 이동 생성될 수 있음을 명시해요 (콘셉트) [edit] |
is_copy_constructible_type is_trivially_copy_constructible_type is_nothrow_copy_constructible_type (C++26) (C++26) (C++26) |
반영된 타입에 복사 생성자가 있는지 확인해요 (함수) [edit] |