Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(C++14) |
Defined in header <type_traits> | ||
template<class From,class To> struct is_convertible; | (1) | (since C++11) |
template<class From,class To> struct is_nothrow_convertible; | (2) | (since C++20) |
To
usingimplicit conversions, or bothFrom
andTo
are possibly cv-qualifiedvoid), provides the member constantvalue equal totrue. Otherwisevalue isfalse. For the purposes of this check, the use ofstd::declval in the return statement is not considered anODR-use.If | (since C++26) |
IfFrom
orTo
is not a complete type, (possibly cv-qualified)void, or an array of unknown bound, the behavior is undefined.
If an instantiation of a template above depends, directly or indirectly, on an incomplete type, and that instantiation could yield a different result if that type were hypothetically completed, the behavior is undefined.
If the program adds specializations for any of the templates described on this page, the behavior is undefined.
Contents |
template<class From,class To> constexprbool is_convertible_v= is_convertible<From, To>::value; | (since C++17) | |
template<class From,class To> constexprbool is_nothrow_convertible_v= is_nothrow_convertible<From, To>::value; | (since C++20) | |
value [static] | true ifFrom is convertible toTo ,false otherwise(public static member constant) |
operator bool | converts the object tobool, returnsvalue (public member function) |
operator() (C++14) | returnsvalue (public member function) |
Type | Definition |
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{}; |
Gives well-defined results for reference types, void types, array types, and function types.
Currently the standard has not specified whether the destruction of the object produced by the conversion (either a result object or a temporary bound to a reference) is considered as a part of the conversion. This isLWG issue 3400.
All known implementations treat the destruction as a part of the conversion, as proposed inP0758R1.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__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*>);// Note that the Perfect Forwarding constructor makes the class E be// "convertible" from everything. So, A is replaceable by 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){ifconstexpr(std::is_convertible_v<T,std::string> or std::is_convertible_v<T,std::string_view>)return x;elsereturnstd::to_string(x);}; using std::operator"" s, std::operator"" sv;constchar* 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';}
Output:
"one" "two" "three" "42" "42.000000"
(C++11) | checks if a type is a base of the other type (class template)[edit] |
checks if a type is apointer-interconvertible (initial) base of another type (class template)[edit] | |
checks if objects of a type arepointer-interconvertible with the specified subobject of that type (function template)[edit] | |
(C++20) | specifies that a type is implicitly convertible to another type (concept)[edit] |