C++标准模板(STL)- 概念库 (C++20) - 指定能复制构造和移动构造一个类型的对象 - (std::copy_constructible)

概念库提供基础语言概念的定义,它们能用于进行模板实参的编译时校验,以及基于类型属性的函数派发。这些概念在程序中提供等式推理的基础。

标准库中的大多数概念一同加上了语法及语义要求。通常,编译器只能检查语法要求。若在使用点语义要求未得到满足,则程序为病式,不要求诊断。

类型支持(基本类型、RTTI、类型特性)

指定能复制构造和移动构造一个类型的对象

std::copy_constructible

定义于头文件 <concepts>

template <class T>

concept copy_constructible =
  std::move_constructible<T> &&
  std::constructible_from<T, T&> && std::convertible_to<T&, T> &&
  std::constructible_from<T, const T&> && std::convertible_to<const T&, T> &&

  std::constructible_from<T, const T> && std::convertible_to<const T, T>;
(C++20 起)

概念 copy_constructible 若符合这些条件则得到满足: T 为左值引用类型,或若它是 move_constructible 对象类型,而能从(可为 const 的)该类型左值或 const 右值,在直接和复制初始化语境中以通常语义构造该类型对象(构造副本而不更改源)。

更精确而言,若 T 为对象类型,则 copy_constructible<T> 仅若符合下列条件才得到满足。给定

  • vT 类型左值(可为 const )或为 const T 类型右值,

下列为真:

  • 定义 T u = v; 后, u 等于 v
  • T(v) 等于 v

参阅

is_copy_constructible

is_trivially_copy_constructible

is_nothrow_copy_constructible

(C++11)

检查类型是否拥有复制构造函数
(类模板)

调用示例

#include <iostream>
#include <type_traits>

class E
{
public:
    template<class T> E(T&&) { }
};

class A {};
class B : public A {};
class C {};
class D
{
public:
    operator C()
    {
        return c;
    }  C c;
};

//自定义的实现
namespace std
{

template< class T, class... Args >
const bool is_constructible_v = is_constructible<T, Args...>::value;

template< class T, class... Args >
const bool is_trivially_constructible_v = is_trivially_constructible<T, Args...>::value;

template< class T, class... Args >
const bool is_nothrow_constructible_v = is_nothrow_constructible<T, Args...>::value;

template< class T >
const bool is_nothrow_destructible_v = is_nothrow_destructible<T>::value;

template < class T >
const bool destructible = is_nothrow_destructible<T>::value;

template <class From, class To>
constexpr bool convertible_to = (std::is_convertible<From, To>::value);

template < class T, class... Args >
const bool constructible_from = (destructible<T> && is_constructible_v<T, Args...>);

template< class T >
const bool move_constructible = (constructible_from<T, T> && convertible_to<T, T>);

template <class T>
const bool copy_constructible =
    move_constructible<T> &&
    constructible_from<T, T&> && convertible_to<T&, T> &&
    constructib
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值