Namespaces
Variants

std:: is_convertible, std:: is_nothrow_convertible

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 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 ),则提供等于 true 的成员常量 value 。否则 value false 。为此检查之目的,return 语句中对 std::declval 的使用不被视为 ODR 使用

To 是引用类型,且将 std:: declval < From > ( ) 绑定到 To 时会创建 临时对象 ,则假想函数中的 return 语句仍被视为良构,尽管在实际函数中此类绑定是病式的。

(since 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
[static]
From 可转换为 To 则为 true ,否则为 false
(公开静态成员常量)

成员函数

operator bool
将对象转换为 bool ,返回 value
(公开成员函数)
operator()
(C++14)
返回 value
(公开成员函数)

成员类型

类型 定义
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 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*>);
    // 注意完美转发构造函数使得类 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)
检查一个类型是否是另一个类型的基类
(类模板)
检查一个类型是否是另一个类型的 指针可互转换 (初始)基类
(类模板)
检查某类型的对象是否与该类型的指定子对象 指针可互转换
(函数模板)
指定一个类型可隐式转换为另一个类型
(概念)