Namespaces
Variants
Views
Actions

std::is_convertible

From cppreference.com
< cpp‎ | types
Revision as of 08:35, 11 November 2015 by T. Canens (Talk | contribs)

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <type_traits>
template< class From, class To >
struct is_convertible;
(since C++11)

If the return statement in the imaginary function definition To test() { return std::declval<From>(); } is well-formed, (that is, if std::declval<From>() can be converted to To using implicit conversion), provides the member constant value equal to true. Otherwise value is false. For the purposes of this check, the use of std::declval is in the return statement is not considered an odr-use.

If From or To 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.

Contents

Helper variable template

template< class From, class To >
constexpr bool is_convertible_v = is_convertible<From, To>::value;
(since C++17)

Inherited from std::integral_constant

Member constants

value
[static]
true if From is convertible to To , false otherwise
(public static member constant)

Member functions

operator bool
converts the object to bool, returns value
(public member function)
operator()
(C++14)
returns value
(public member function)

Member types

Type Definition
value_type bool
type std::integral_constant<bool, value>

Notes

Gives well-defined results for reference types, void types, array types, and function types.

Example

#include <iostream>
#include <type_traits>
 
int main() 
{
    class A {};
    class B : public A {};
    class C {};
 
    bool b2a = std::is_convertible<B*, A*>::value;
    bool a2b = std::is_convertible<A*, B*>::value;
    bool b2c = std::is_convertible<B*, C*>::value;
 
    std::cout << std::boolalpha;
    std::cout << b2a << '\n';
    std::cout << a2b << '\n';
    std::cout << b2c << '\n';
}

Output:

true
false
false

See also

(library fundamentals TS)
variable template alias of std::is_convertible::value
(variable template)[edit]