为什么带有非const复制构造函数的类不被视为可复制构造? [英] Why is class with non-const copy constructor not treated as copy constructible?

查看:79
本文介绍了为什么带有非const复制构造函数的类不被视为可复制构造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出

struct Foo 
{
    Foo(Foo&) {} 
};

std :: is_copy_constructible< Foo> :: value false

Foo具有有效的复制构造函数:来自草稿n4659:

Foo has valid copy-constructor: From 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 )。

为什么是类

推荐答案

虽然这确实是有效的副本构造函数,但<$ c定义$ c> is_copy_constructible 类型特征以提供与 is_constructible_v 相同的结果,因为它旨在对应于 CopyConstructible 概念,该概念也由标准定义。

While this is indeed a valid copy constructor, the is_copy_constructible type-trait is defined to give the same result as is_constructible_v<T, const T&>, because it is intended to correspond to the CopyConstructible concept that is also defined by the standard.

[ Utility.arg.requirements] / 1 它表示


C ++标准库中的模板定义引用了各种命名的需求,这些需求的表20–27列出了详细信息。在这些表中, T 是实例化模板的C ++程序要提供的对象或引用类型; [...] v是类型的左值(可能是const )T或const T类型的右值。

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概念在表24

The CopyConstructible concept is defined in Table 24 as


表24 — CopyConstructible要求(除了MoveConstructible之外)

表达式   后置条件

T u = v;          v的值保持不变,并且等于u

T(v)     v的值不变,等于T(v)

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 的要求之一,不认为是这样。

Therefore, since your object is not constructible from a const Foo lvalue, which is one of the requirements of CopyConstructible, it is not regarded as such.

这篇关于为什么带有非const复制构造函数的类不被视为可复制构造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆