std::is_convertible, std::is_nothrow_convertible
来自 cppreference.cn
定义于头文件 <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 起) |
1) 如果虚构函数定义 To test() { return std::declval<From>(); } 格式良好(即,std::declval<From>() 可以使用隐式转换转换为
To
,或者 From
和 To
都是可能经过 cv 限定的 void),则提供成员常量 value 等于 true。否则 value 为 false。为了此检查的目的,返回语句中 std::declval 的使用不被视为 ODR 使用。
如果 |
(C++26 起) |
访问检查的执行方式如同来自与两种类型均无关的上下文。只考虑返回语句中表达式的直接上下文的有效性(包括对返回类型的转换)。
2) 同 (1),但转换也是 noexcept 的。
如果 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 [静态] |
true 如果 From 可以转换为 To ,否则为 false(public static 成员常量) |
成员函数
operator bool |
将对象转换为 bool,返回 value (公开成员函数) |
operator() (C++14) |
返回 value (公开成员函数) |
成员类型
类型 | 定义 |
value_type
|
bool |
类型
|
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 issue 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*>); // 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) { 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) |
指定类型可隐式转换为另一类型 (概念) |