概念库提供基础语言概念的定义,它们能用于进行模板实参的编译时校验,以及基于类型属性的函数派发。这些概念在程序中提供等式推理的基础。
标准库中的大多数概念一同加上了语法及语义要求。通常,编译器只能检查语法要求。若在使用点语义要求未得到满足,则程序为病式,不要求诊断。
类型支持(基本类型、RTTI、类型特性)
指定能移动构造一个类型的对象
std::move_constructible
定义于头文件 | ||
template< class T > | (C++20 起) |
概念 move_constructible
若符合这些条件则得到满足, T
为引用类型,或它是对象类型,并且能在直接和复制初始化语境中,以通常语义从该类型右值构造该类型对象。
更精确而言,若 T
是对象类型,则 move_constructible<T>
仅若符合下列条件才得到满足。给定
rv
,T
类型右值,和u2
,等于rv
的T
类型不同对象,
下列为真:
- 在定义 T u = rv; 后,
u
等于u2
; T(rv)
等于u2
;且- 若
T
无 const 限定,则rv
的结果状态(在上述任一条中求值的定义/表达式后)合法但未指定;否则不更改它。
参阅
is_move_constructible is_trivially_move_constructible is_nothrow_move_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>);
}
int main()
{
std::cout << std::boolalpha;
std::cout << "std::is_convertible<B*, A*>::value: "
<< std::is_convertible<B*, A*>::value << std::endl;
std::cout << "std::is_convertible<A*, B*>::value: "
<< std::is_convertible<A*, B*>::value << std::endl;
std::cout << "std::is_convertible<B*, C*>::value: "
<< std::is_convertible<B*, C*>::value << std::endl;
std::cout << "std::is_convertible<D, C>::value: "
<< std::is_convertible<D, C>::value << std::endl;
std::cout << "std::is_convertible<E, A>::value: "
<< std::is_convertible<E, A>::value << std::endl;
std::cout << "std::is_convertible<int, double>::value: "
<< std::is_convertible<int, double>::value << std::endl;
std::cout << "std::is_convertible<int, char>::value: "
<< std::is_convertible<int, char>::value << std::endl;
std::cout << "std::is_convertible<std::string, char>::value:"
<< std::is_convertible<std::string, char>::value << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::endl;
std::cout << "std::destructible<A>: "
<< std::destructible<A> << std::endl;
std::cout << "std::destructible<B>: "
<< std::destructible<B> << std::endl;
std::cout << "std::destructible<C>: "
<< std::destructible<C> << std::endl;
std::cout << "std::destructible<D>: "
<< std::destructible<D> << std::endl;
std::cout << "std::destructible<E>: "
<< std::destructible<E> << std::endl;
std::cout << "std::destructible<float>: "
<< std::destructible<float> << std::endl;
std::cout << "std::destructible<int>: "
<< std::destructible<int> << std::endl;
std::cout << "std::destructible<bool>: "
<< std::destructible<bool> << std::endl;
std::cout << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::endl;
std::cout << "std::constructible_from<B, A>: "
<< std::constructible_from<B, A> << std::endl;
std::cout << "std::constructible_from<A, B>: "
<< std::constructible_from<A, B> << std::endl;
std::cout << "std::constructible_from<B*, A*>: "
<< std::constructible_from<B*, A*> << std::endl;
std::cout << "std::constructible_from<A*, B*>: "
<< std::constructible_from<A*, B*> << std::endl;
std::cout << "std::constructible_from<B*, C*>: "
<< std::constructible_from<B*, C*> << std::endl;
std::cout << "std::constructible_from<D, C>: "
<< std::constructible_from<D, C> << std::endl;
std::cout << "std::constructible_from<E, A>: "
<< std::constructible_from<E, A> << std::endl;
std::cout << "std::constructible_from<int, double>: "
<< std::constructible_from<int, double> << std::endl;
std::cout << "std::constructible_from<int, char>: "
<< std::constructible_from<int, char> << std::endl;
std::cout << "std::constructible_from<std::string, char>: "
<< std::constructible_from<std::string, char> << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::endl;
std::cout << "std::convertible_to<B, A>: "
<< std::convertible_to<B, A> << std::endl;
std::cout << "std::convertible_to<A, B>: "
<< std::convertible_to<A, B> << std::endl;
std::cout << "std::convertible_to<B*, A*>: "
<< std::convertible_to<B*, A*> << std::endl;
std::cout << "std::convertible_to<A*, B*>: "
<< std::convertible_to<A*, B*> << std::endl;
std::cout << "std::convertible_to<B*, C*>: "
<< std::convertible_to<B*, C*> << std::endl;
std::cout << "std::convertible_to<D, C>: "
<< std::convertible_to<D, C> << std::endl;
std::cout << "std::convertible_to<E, A>: "
<< std::convertible_to<E, A> << std::endl;
std::cout << "std::convertible_to<int, double>: "
<< std::convertible_to<int, double> << std::endl;
std::cout << "std::convertible_to<int, char>: "
<< std::convertible_to<int, char> << std::endl;
std::cout << "std::convertible_to<std::string, char>: "
<< std::convertible_to<std::string, char> << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::endl;
std::cout << "std::move_constructible<B>: "
<< std::move_constructible<B> << std::endl;
std::cout << "std::move_constructible<A>: "
<< std::move_constructible<A> << std::endl;
std::cout << "std::move_constructible<B*>: "
<< std::move_constructible<B*> << std::endl;
std::cout << "std::move_constructible<A*>: "
<< std::move_constructible<A*> << std::endl;
std::cout << "std::move_constructible<C*>: "
<< std::move_constructible<C*> << std::endl;
std::cout << "std::move_constructible<D>: "
<< std::move_constructible<D> << std::endl;
std::cout << "std::move_constructible<E>: "
<< std::move_constructible<E> << std::endl;
std::cout << "std::move_constructible<int>: "
<< std::move_constructible<int> << std::endl;
std::cout << "std::move_constructible<char>: "
<< std::move_constructible<char> << std::endl;
std::cout << "std::move_constructible<std::string>: "
<< std::move_constructible<std::string> << std::endl;
return 0;
}
输出
std::is_convertible<B*, A*>::value: true
std::is_convertible<A*, B*>::value: false
std::is_convertible<B*, C*>::value: false
std::is_convertible<D, C>::value: true
std::is_convertible<E, A>::value: false
std::is_convertible<int, double>::value: true
std::is_convertible<int, char>::value: true
std::is_convertible<std::string, char>::value:false
-----------------------------------------------
std::destructible<A>: true
std::destructible<B>: true
std::destructible<C>: true
std::destructible<D>: true
std::destructible<E>: true
std::destructible<float>: true
std::destructible<int>: true
std::destructible<bool>: true
-----------------------------------------------
std::constructible_from<B, A>: false
std::constructible_from<A, B>: true
std::constructible_from<B*, A*>: false
std::constructible_from<A*, B*>: true
std::constructible_from<B*, C*>: false
std::constructible_from<D, C>: false
std::constructible_from<E, A>: true
std::constructible_from<int, double>: true
std::constructible_from<int, char>: true
std::constructible_from<std::string, char>: false
-----------------------------------------------
std::convertible_to<B, A>: true
std::convertible_to<A, B>: false
std::convertible_to<B*, A*>: true
std::convertible_to<A*, B*>: false
std::convertible_to<B*, C*>: false
std::convertible_to<D, C>: true
std::convertible_to<E, A>: false
std::convertible_to<int, double>: true
std::convertible_to<int, char>: true
std::convertible_to<std::string, char>: false
-----------------------------------------------
std::move_constructible<B>: true
std::move_constructible<A>: true
std::move_constructible<B*>: true
std::move_constructible<A*>: true
std::move_constructible<C*>: true
std::move_constructible<D>: true
std::move_constructible<E>: true
std::move_constructible<int>: true
std::move_constructible<char>: true
std::move_constructible<std::string>: true