Namespaces
Variants

std:: is_assignable, std:: is_trivially_assignable, std:: is_nothrow_assignable

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 U >
struct is_assignable ;
(1) (C++11부터)
template < class T, class U >
struct is_trivially_assignable ;
(2) (C++11부터)
template < class T, class U >
struct is_nothrow_assignable ;
(3) (C++11부터)
1) 표현식 std:: declval < T > ( ) = std:: declval < U > ( ) 가 평가되지 않은 컨텍스트에서 유효할 경우, 멤버 상수 value true 와 같게 제공합니다. 그렇지 않으면, value false 입니다. 접근 검사 는 두 타입 모두와 관련 없는 컨텍스트에서 수행되는 것처럼 이루어집니다.
2) (1) 과 동일하지만, 할당 표현식의 평가는 trivial하지 않은 연산을 호출하지 않습니다. 이 검사의 목적상, std::declval 호출은 trivial한 것으로 간주되며 std::declval odr-use 로 간주되지 않습니다.
3) (1) 과 동일하지만, 할당 표현식의 평가가 noexcept가 아닌 연산을 호출하지 않습니다.

만약 T 또는 U 가 완전한 타입이 아니거나, (cv 한정자가 있을 수 있는) void , 또는 크기를 알 수 없는 배열인 경우, 동작은 정의되지 않습니다.

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

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

목차

헬퍼 변수 템플릿

template < class T, class U >
constexpr bool is_assignable_v = is_assignable < T, U > :: value ;
(C++17부터)
template < class T, class U >
constexpr bool is_trivially_assignable_v = is_trivially_assignable < T, U > :: value ;
(C++17부터)
template < class T, class U >
constexpr bool is_nothrow_assignable_v = is_nothrow_assignable < T, U > :: value ;
(C++17부터)

std:: integral_constant 로부터 상속됨

멤버 상수

value
[static]
true 만약 T U 로부터 할당 가능한 경우, 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 또는 U 의 사용이 템플릿 특수화, 암시적으로 정의된 특수 멤버 함수의 생성 등을 트리거하는 경우, 그리고 그것들에 오류가 있다면, std :: is_assignable < T,U > :: value 가 컴파일되어 true 로 평가되더라도 실제 대입은 컴파일되지 않을 수 있습니다.

예제

#include <iostream>
#include <string>
#include <type_traits>
struct Ex1 { int n; };
int main()
{
    std::cout << std::boolalpha
              << "int is assignable from int? "
              << std::is_assignable<int, int>::value << '\n' // 1 = 1; wouldn't compile
              << "int& is assignable from int? "
              << std::is_assignable<int&, int>::value << '\n' // int a; a = 1; works
              << "int is assignable from double? "
              << std::is_assignable<int, double>::value << '\n'
              << "int& is nothrow assignable from double? "
              << std::is_nothrow_assignable<int&, double>::value << '\n'
              << "string is assignable from double? "
              << std::is_assignable<std::string, double>::value << '\n'
              << "Ex1& is trivially assignable from const Ex1&? "
              << std::is_trivially_assignable<Ex1&, const Ex1&>::value << '\n';
}

출력:

int is assignable from int? false
int& is assignable from int? true
int is assignable from double? false
int& is nothrow assignable from double? true
string is assignable from double? true
Ex1& is trivially assignable from const Ex1&? true

참고 항목

타입이 복사 대입 연산자를 가지는지 검사합니다
(클래스 템플릿)
타입이 이동 대입 연산자를 가지는지 검사합니다
(클래스 템플릿)
타입이 다른 타입으로부터 대입 가능함을 명시합니다
(컨셉)