std:: is_convertible, std:: is_nothrow_convertible
|
헤더 파일에 정의됨
<type_traits>
|
||
|
template
<
class
From,
class
To
>
struct is_convertible ; |
(1) | (C++11부터) |
|
template
<
class
From,
class
To
>
struct is_nothrow_convertible ; |
(2) | (C++20부터) |
To
로 변환 가능하거나,
From
과
To
가 모두 가능하게 cv-qualified된
void
인 경우),
value
멤버 상수를
true
로 제공합니다. 그렇지 않으면
value
는
false
입니다. 이 검사 목적상, 반환문에서
std::declval
의 사용은
ODR-use
로 간주되지 않습니다.
|
만약
|
(C++26부터) |
만약
From
또는
To
가 완전한 타입이 아니거나, (cv 한정자가 있을 수 있음)
void
, 또는 알려지지 않은 경계의 배열인 경우, 동작은 정의되지 않습니다.
템플릿의 인스턴스화가 직접적 또는 간접적으로 불완전한 타입에 의존하고, 해당 타입이 가상적으로 완성되었을 때 다른 결과를 초래할 수 있는 경우, 그 동작은 정의되지 않습니다.
프로그램이 이 페이지에 설명된 템플릿들 중 어느 것에 대해 특수화를 추가하는 경우, 동작은 정의되지 않습니다.
목차 |
헬퍼 변수 템플릿
|
template
<
class
From,
class
To
>
constexpr bool is_convertible_v = is_convertible < From, To > :: value ; |
(C++17부터) | |
|
template
<
class
From,
class
To
>
constexpr bool is_nothrow_convertible_v = is_nothrow_convertible < From, To > :: value ; |
(C++20부터) | |
std:: integral_constant 에서 상속됨
멤버 상수
|
value
[static]
|
true
만약
From
이
To
로 변환 가능한 경우,
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_convertible
(1)
|
|---|
namespace detail { template<class T> auto test_returnable(int) -> decltype( void(static_cast<T(*)()>(nullptr)), std::true_type{} ); template<class> auto test_returnable(...) -> std::false_type; template<class From, class To> auto test_implicitly_convertible(int) -> decltype( void(std::declval<void(&)(To)>()(std::declval<From>())), std::true_type{} ); template<class, class> auto test_implicitly_convertible(...) -> std::false_type; } // namespace detail template<class From, class To> struct is_convertible : std::integral_constant<bool, (decltype(detail::test_returnable<To>(0))::value && decltype(detail::test_implicitly_convertible<From, To>(0))::value) || (std::is_void<From>::value && std::is_void<To>::value) > {}; |
is_nothrow_convertible
(2)
|
template<class From, class To> struct is_nothrow_convertible : std::conjunction<std::is_void<From>, std::is_void<To>> {}; template<class From, class To> requires requires { static_cast<To(*)()>(nullptr); { std::declval<void(&)(To) noexcept>()(std::declval<From>()) } noexcept; } struct is_nothrow_convertible<From, To> : std::true_type {}; |
참고 사항
참조 타입, void 타입, 배열 타입, 함수 타입에 대해 명확하게 정의된 결과를 제공합니다.
현재 표준은 변환에 의해 생성된 객체(결과 객체 또는 참조에 바인딩된 임시 객체)의 파괴가 변환의 일부로 간주되는지 여부를 명시하지 않았습니다. 이는 LWG 이슈 3400 입니다.
모든 알려진 구현들은 변환의 일부로 소멸을 처리하며, 이는 P0758R1 에서 제안된 바와 같습니다.
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_is_nothrow_convertible
|
201806L
|
(C++20) |
std::is_nothrow_convertible
|
예제
#include <iomanip> #include <iostream> #include <string> #include <string_view> #include <type_traits> class E { public: template<class T> E(T&&) {} }; int main() { class A {}; class B : public A {}; class C {}; class D { public: operator C() { return c; } C c; }; static_assert(std::is_convertible_v<B*, A*>); static_assert(!std::is_convertible_v<A*, B*>); static_assert(std::is_convertible_v<D, C>); static_assert(!std::is_convertible_v<B*, C*>); // 완벽한 전달 생성자는 클래스 E를 모든 타입으로부터 // "변환 가능"하게 만듭니다. 따라서 A는 B, C, D 등으로 대체 가능합니다: static_assert(std::is_convertible_v<A, E>); static_assert(!std::is_convertible_v<std::string_view, std::string>); static_assert(std::is_convertible_v<std::string, std::string_view>); auto stringify = []<typename T>(T x) { if constexpr (std::is_convertible_v<T, std::string> or std::is_convertible_v<T, std::string_view>) return x; else return std::to_string(x); }; using std::operator "" s, std::operator "" sv; const char* three = "three"; std::cout << std::quoted(stringify("one"s)) << ' ' << std::quoted(stringify("two"sv)) << ' ' << std::quoted(stringify(three)) << ' ' << std::quoted(stringify(42)) << ' ' << std::quoted(stringify(42.0)) << '\n'; }
출력:
"one" "two" "three" "42" "42.000000"
참고 항목
|
(C++11)
|
한 타입이 다른 타입의 기반 클래스인지 검사합니다
(클래스 템플릿) |
|
한 타입이 다른 타입의
포인터 상호 변환 가능한
(초기) 기반 클래스인지 검사합니다
(클래스 템플릿) |
|
|
한 타입의 객체들이 해당 타입의 지정된 하위 객체와
포인터 상호 변환 가능한지
검사합니다
(함수 템플릿) |
|
|
(C++20)
|
한 타입이 다른 타입으로 암시적으로 변환 가능함을 명시합니다
(컨셉) |