[英]Why is class with non-const copy constructor not treated as copy constructible?
特定
struct Foo
{
Foo(Foo&) {}
};
std::is_copy_constructible<Foo>::value
为false
Foo有有效的拷贝构造函数:来自草案n4659:
15.8.1 Copy/move constructors [class.copy.ctor]
1
A non-template constructor for class X is a copy constructor if its first parameter is of type X& , const X& ,
volatile X& or const volatile X& , and either there are no other parameters or else all other parameters
have default arguments (11.3.6). [Example: X::X(const X&) and X::X(X&,int=1) are copy constructors.
但is_copy_constructible
根据标准测试is_constructible_v<T, const T&>
( const )。
为什么具有非const复制构造函数的类不被视为可复制构造?
虽然这确实是一个有效的复制构造函数,但is_copy_constructible
类型特征被定义为提供与is_constructible_v<T, const T&>
相同的结果,因为它旨在对应于也由标准定义的CopyConstructible
概念。
在[utility.arg.requirements] / 1中说
C ++标准库中的模板定义是指各种命名需求,其详细信息在表20-27中列出。 在这些表中,
T
是由实例化模板的C ++程序提供的对象或引用类型; [...]和v是类型( 可能是const )T的左值或类型为const T的右值。
CopyConstructible概念在表24中定义为
表24 - CopyConstructible要求(除MoveConstructible外)
表达后置条件
T u = v; v的值不变,相当于u
T(v)v的值不变,相当于T(v)
因此,由于你的对象不能从const Foo
左值开始构造,这是CopyConstructible
的要求之一,因此它不被认为是这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.