C++ std::is_convertible模板用法及代码示例

<type_traits>头文件中提供了C++ STL的std::is_convertible模板。 C++ STL的std::is_convertible模板用于检查是否可以将任何数据类型A隐式转换为任何数据类型B。它返回布尔值true或false。

头文件:

#include<type_traits>

模板类别:

template< class From, class To >
struct is_convertible;

template< class From, class To >
struct is_nothrow_convertible;

用法:

is_convertible <A*, B*>::value;

参数:它采用A和B两种数据类型:

  • A:它代表要转换的参数。
  • B:它代表参数A隐式转换的参数。

返回值:

  • True:如果将给定的数据类型A转换为数据类型B。
  • False:如果给定的数据类型A没有转换为数据类型B。

下面是演示C++中std::is_convertible的程序:

程序:

// C++ program to illustrate 
// std::is_convertible example 
#include <bits/stdc++.h> 
#include <type_traits> 
using namespace std; 
  
// Given classes 
class A { 
}; 
class B:public A { 
}; 
class C { 
}; 
  
// Driver Code 
int main() 
{ 
    cout << boolalpha; 
  
    // Check if class B is 
    // convertible to A or not 
    bool BtoA = is_convertible<B*, A*>::value; 
    cout << BtoA << endl; 
  
    // Check if class A is 
    // convertible to B or not 
    bool AtoB = is_convertible<A*, B*>::value; 
    cout << AtoB << endl; 
  
    // Check if class B is 
    // convertible to C or not 
    bool BtoC = is_convertible<B*, C*>::value; 
    cout << BtoC << endl; 
  
    // Check if int is convertible 
    // to float or not 
    cout << "int to float:"
         << is_convertible<int, float>::value 
         << endl; 
  
    // Check if int is convertible 
    // to const int or not 
    cout << "int to const int:"
         << is_convertible<int, const int>::value 
         << endl; 
  
    return 0; 
}

输出:

true
false
false
int to float:true
int to const int:true

参考:http://www.cplusplus.com/reference/type_traits/is_convertible/

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++11 中,没有类似于 Rust 中 `std::option` 的标准库类型。但是 C++17 引入了 `std::optional` 类型,它的用法很类似于 Rust 中的 `std::option`。 `std::optional` 也表示一个值可能存在或者不存在的情况。它的定义如下: ```cpp template< class T > class optional { public: // 构造函数 constexpr optional() noexcept; constexpr optional( nullopt_t ) noexcept; constexpr optional( const optional& other ); constexpr optional( optional&& other ) noexcept( std::is_nothrow_move_constructible_v<T> ); template< class... Args > requires std::is_constructible_v<T, Args...> constexpr explicit optional( std::in_place_t, Args&&... args ); template< class U, class... Args > requires std::is_constructible_v<T, std::initializer_list<U>&, Args&&...> constexpr explicit optional( std::in_place_t, std::initializer_list<U> ilist, Args&&... args ); template< class U = T > requires std::is_copy_constructible_v<T> && std::is_convertible_v<const U&, T> constexpr optional( const U& value ); template< class U = T > requires std::is_move_constructible_v<T> && std::is_convertible_v<U&&, T> constexpr optional( U&& value ); template< class U > requires std::is_constructible_v<T, std::initializer_list<U>&> constexpr optional( std::initializer_list<U> ilist ); // 成员函数 constexpr T* operator->(); constexpr const T* operator->() const; constexpr T& operator*(); constexpr const T& operator*() const; constexpr explicit operator bool() const noexcept; constexpr T& value(); constexpr const T& value() const; template< class U > constexpr T value_or( U&& default_value ) const&; template< class U > constexpr T value_or( U&& default_value ) &&; void reset() noexcept; template< class... Args > requires std::is_constructible_v<T, Args&&...> T& emplace( Args&&... args ); template< class U, class... Args > requires std::is_constructible_v<T, std::initializer_list<U>&, Args&&...> T& emplace( std::initializer_list<U> ilist, Args&&... args ); void swap( optional& other ) noexcept( std::is_nothrow_move_constructible_v<T>&& noexcept(std::swap( std::declval<T&>(), std::declval<T&>() ) ) ); }; ``` 其中,`T` 是可能存在的值的类型。如果可能存在的值存在,那么 `optional` 对象就包含这个值;如果可能存在的值不存在,那么 `optional` 对象为空。 `std::optional` 提供了一些方法来处理可能存在的值。例如,可以使用 `value()` 方法获取可能存在的值,如果对象为空则会抛出 `std::bad_optional_access` 异常。也可以使用 `value_or()` 方法获取可能存在的值,如果对象为空则返回一个默认值。还可以使用 `has_value()` 方法来判断一个值是否存在。 例如,下面的代码演示了如何使用 `std::optional`: ```cpp #include <iostream> #include <optional> int main() { std::optional<int> x = 5; std::optional<int> y = std::nullopt; std::cout << "x is " << *x << std::endl; // 输出 x is 5 std::cout << "y is " << y.value_or(0) << std::endl; // 输出 y is 0 if (x.has_value()) { std::cout << "x has value" << std::endl; // 输出 x has value } if (!y.has_value()) { std::cout << "y does not have value" << std::endl; // 输出 y does not have value } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值