gpt4 book ai didi

C++11 最佳参数传递

转载 作者:可可西里 更新时间:2023-11-01 17:37:19 25 4
gpt4 key购买 nike

考虑这些类:

#include <iostream>
#include <string>

class A
{
std::string test;
public:
A (std::string t) : test(std::move(t)) {}
A (const A & other) { *this = other; }
A (A && other) { *this = std::move(other); }

A & operator = (const A & other)
{
std::cerr<<"copying A"<<std::endl;
test = other.test;
return *this;
}

A & operator = (A && other)
{
std::cerr<<"move A"<<std::endl;
test = other.test;
return *this;
}
};

class B
{
A a;
public:
B (A && a) : a(std::move(a)) {}
B (A const & a) : a(a) {}
};

在创建 B 时,我始终为 A 提供最佳前向路径,右值移动一次或左值复制一次。

是否有可能用一个构造函数实现相同的结果?在这种情况下这不是一个大问题,但是多个参数呢?我需要参数列表中所有可能出现的左值和右值的组合。

这不仅限于构造函数,也适用于函数参数(例如 setter)。

注意:本题严格针对class BA 类 的存在只是为了可视化复制/移动调用是如何执行的。

最佳答案

“按值(value)”方法是一种选择。它不像您所拥有的那样最佳,但只需要一个重载:

class B
{
A a;
public:
B (A _a) : a(move(_a)) {}
};

成本是左值和 xvalue 的 1 次额外移动构造,但这对于纯右值(1 次移动)仍然是最佳的。 “xvalue”是已使用 std::move 转换为右值的左值。

你也可以试试“完美转发”的方案:

class B
{
A a;
public:
template <class T,
class = typename std::enable_if
<
std::is_constructible<A, T>::value
>::type>
B (T&& _a) : a(std::forward<T>(_a)) {}
};

这将使您回到最佳数量的复制/移动结构。但是您应该限制模板构造函数,使其不过分通用。您可能更喜欢使用 is_convertible 而不是 is_constructible ,就像我在上面所做的那样。这也是一个单一的构造函数解决方案,但是随着您添加参数,您的约束会变得越来越复杂。

注意:上述约束之所以必要,是因为没有 B 的客户端当他们查询 std::is_constructible<B, their_type>::value 时会得到错误的答案.如果对 B 没有适当的约束,它将错误地回答 true .

我要说的是,这些解决方案中没有一个总是比其他解决方案更好。这里需要进行工程权衡。

关于C++11 最佳参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10472179/

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