概念库提供基础语言概念的定义,它们能用于进行模板实参的编译时校验,以及基于类型属性的函数派发。这些概念在程序中提供等式推理的基础。
标准库中的大多数概念一同加上了语法及语义要求。通常,编译器只能检查语法要求。若在使用点语义要求未得到满足,则程序为病式,不要求诊断。
类型支持(基本类型、RTTI、类型特性)
指定能复制构造和移动构造一个类型的对象
std::copy_constructible
定义于头文件 |
||
template <class T> concept copy_constructible = |
(C++20 起) |
概念 copy_constructible
若符合这些条件则得到满足: T
为左值引用类型,或若它是 move_constructible 对象类型,而能从(可为 const 的)该类型左值或 const 右值,在直接和复制初始化语境中以通常语义构造该类型对象(构造副本而不更改源)。
更精确而言,若 T
为对象类型,则 copy_constructible<T>
仅若符合下列条件才得到满足。给定
v
,T
类型左值(可为 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