所有的CopyConstructible类型都是MoveConstructible类型吗? [英] Are all CopyConstructible types MoveConstructible types?

查看:113
本文介绍了所有的CopyConstructible类型都是MoveConstructible类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据工作草案N3337(与已发布的ISOC ++ 11标准最相似的草案)和

According to the working draft N3337 (the most similar draft to the published ISOC++11 standard) and cppreference.com, the answer is yes.

N3337:


表21 — CopyConstructible要求(除了
MoveConstructible之外)[copyconstructible] [...]

Table 21 — CopyConstructible requirements (in addition to MoveConstructible) [copyconstructible] [...]

cppreference.com


如果类型T满足MoveConstructible,并且[...]类型T满足CopyConstructible,

The type T satisfies CopyConstructible if



但是根据用gcc编译main.cpp的结果(Ubuntu 4.8.4-2ubuntu1 〜14.04)4.8.4,并在Ubuntu 14.04.3 LTS中使用带引号的语句运行a.out,答案不是。

But according to the result of compiling main.cpp with gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4 and running a.out with the quoted statements in Ubuntu 14.04.3 LTS, the answer is not.

main.cpp:

#include <iostream>
#include <type_traits>

struct As
{
    As()=default;
    As(As&&)=delete;
    As(const As&)=default;
    As& operator=(As&&)=delete;
    As& operator=(const As&)=delete;
    ~As()=default;
};

int main()
{
    std::cout<<std::is_move_constructible<As>::value<<std::endl;
    std::cout<<std::is_copy_constructible<As>::value<<std::endl;

    return 0;
}

从终端编译并运行:

$ g++ -std=c++11 main.cpp
$ ./a.out

结果(输出):

0
1

我是否误解了某些内容,或者是N3337和 cppreference.com 错误,还是gcc包含错误?

Did I misunderstand something or is N3337 and cppreference.com wrong, or does gcc contains a bug?

推荐答案

std :: is_copy_constructible< T> 定义为恰好 std :: is_constructible< T,const T&> ; ,即它仅测试从const左值构造是否可行,而不测试CopyConstructible概念的所有属性。

std::is_copy_constructible<T> is defined to be exactly std::is_constructible<T, const T&> i.e. it only tests that construction from a const lvalue is possible, it doesn't test all the properties of the CopyConstructible concept.

所以您的测试没有显示您认为的内容。您的类型不是CopyConstructible类型,因为它没有满足其他一些要求。

So your test does not show what you think it shows. Your type is not a CopyConstructible type, because it fails some of the other requirements.

对于原始问题,是的。因为所有CopyConstructible类型都必须满足MoveConstructible的要求,所以它们都是MoveConstructible类型。 MoveConstructible不需要移动任何东西,只有从右值进行构造是可能的,并且所有CopyConstructible类型都可以从右值进行构造(即使它们可能进行深层复制而不是移动)。

As for the original question, yes. Because all CopyConstructible types must meet the requirements for MoveConstructible they are all MoveConstructible types. MoveConstructible doesn't require that anything gets moved, only that construction from rvalues is possible, and all CopyConstructible types can be constructed from rvalues (even if they might do a deep copy not a move).

您可以创建可以从左值复制但不能从右值复制的异常类型,或者可以从const左值复制但不能从非const左值复制,以及其他可憎的类型。此类类型不是CopyConstructible,并且不能与C ++标准库一起很好地工作。很少有充分的理由创建这样的错误类型。

You can create perverse types that can be copied from lvalues but not from rvalues, or can be copied from const lvalues but not non-const lvalues, and other abominations. Such types are not CopyConstructible, and do not work well with the C++ standard library. There are very few good reasons to ever create perverse types like that.

这篇关于所有的CopyConstructible类型都是MoveConstructible类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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