给定
struct Foo
{
Foo(Foo&) {}
};
std::is_copy_constructible<Foo>::value
是false
Foo 具有有效的复制构造函数:来自 draft 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) 根据标准。
为什么具有非常量复制构造函数的类不被视为可复制构造?
最佳答案
虽然这确实是一个有效的复制构造函数,is_copy_constructible
type-trait 被定义为给出与 is_constructible_v<T, const T&>
相同的结果,因为它旨在对应于 CopyConstructible
标准也定义了这个概念。
在[utility.arg.requirements]/1它说
The template definitions in the C++ standard library refer to various named requirements whose details are set out in Tables 20–27. In these tables,
T
is an object or reference type to be supplied by a C++ program instantiating a template;[...] and v is an lvalue of type (possibly const) T or an rvalue of type const T.
CopyConstructible 概念在 Table 24 中定义作为
Table 24 — CopyConstructible requirements (in addition to MoveConstructible)
Expression Post-condition
T u = v; the value of v is unchanged and is equivalent to u
T(v) the value of v is unchanged and is equivalent to T(v)
因此,由于您的对象不能从 const Foo
构造左值,这是 CopyConstructible
的要求之一, 它不被认为是这样。
关于c++ - 为什么具有非常量复制构造函数的类不被视为可复制构造?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43606777/