gpt4 book ai didi

C++11 - 元组和移动语义

转载 作者:太空狗 更新时间:2023-10-29 20:26:20 26 4
gpt4 key购买 nike

这个示例代码应该编译吗?clangg++ 接受它,而 icc 14 拒绝这样做,提示 auto t = ... std::unique_ptr 的复制构造函数未定义。

#include <iostream>
#include <memory>
#include <tuple>

std::tuple<std::unique_ptr<int[]>, int> foo()
{
std::unique_ptr<int[]> a;
unsigned int b;
auto t = std::make_tuple(std::move(a), b);
return std::move(t);
}

int main()
{
foo();
}

最佳答案

我认为它应该编译:std::make_tuple() 的结果是临时的 std::tuple<T...>它应该移动构建其成员。更准确地说,std::tuple<T...>的移动构造函数是默认的,这应该导致成员移动构造。

显然,没有真正需要分配 std::make_tuple() 的结果至 t .如果你选择引入这个变量,你不应该std::move(t)返回时:当 return 中的表达式时statement 是局部变量,无论如何它都被视为右值。额外的 std::move()但是,禁止复制/移动省略。

关于C++11 - 元组和移动语义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20110427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com