c++ - 如果方法的调用者不需要数据的所有权,有什么好的方法可以避免复制?

标签 c++ c++11 c++14 continuation-passing

这是我最近在思考的问题。假设我们的接口(interface)是一个返回对象的成员函数,该对象的复制成本高而移动成本低(std::string、std::vector 等)。一些实现可能会计算结果并返回一个临时对象,而其他实现可能只是返回一个成员对象。

示例代码说明:

// assume the interface is: Vec foo() const
// Vec is cheap to move but expensive to copy

struct RetMember {
    Vec foo() const { return m_data; }
    Vec m_data;
    // some other code
}

struct RetLocal {
    Vec foo() const {
        Vec local = /*some computation*/;
        return local;
    }
};

还有各种“客户端”。有些只读取数据,有些需要所有权。

void only_reads(const Vec&) { /* some code */ }
void requires_ownership(Vec) { /* some code */ }

上面的代码组合得很好,但没有达到应有的效率。以下是所有组合:

RetMember retmem;
RetLocal retloc;

only_reads(retmem.foo()); // unnecessary copy, bad
only_reads(retloc.foo()); // no copy, good

requires_ownership(retmem.foo()); // copy, good
requires_ownership(retloc.foo()); // no copy, good

解决这种情况的好方法是什么?

我想到了两种方法,但我确信有更好的解决方案。

在我的第一次尝试中,我写了一个 DelayedCopy 包装器,它保存一个 T 的值或一个指向 const T 的指针。它非常丑陋,需要额外的努力,引入了冗余的移动,阻碍了复制省略,并且可能有很多其他问题。

我的第二个想法是 continuation-passing style ,它工作得很好,但将成员函数转换为成员函数模板。我知道,有 std::function,但它有其开销,因此在性能方面它可能是 Not Acceptable 。

示例代码:

#include <boost/variant/variant.hpp>
#include <cstdio>
#include <iostream>
#include <type_traits>

struct Noisy {

  Noisy() = default;
  Noisy(const Noisy &) { std::puts("Noisy: copy ctor"); }
  Noisy(Noisy &&) { std::puts("Noisy: move ctor"); }

  Noisy &operator=(const Noisy &) {
    std::puts("Noisy: copy assign");
    return *this;
  }
  Noisy &operator=(Noisy &&) {
    std::puts("Noisy: move assign");
    return *this;
  }
};

template <typename T> struct Borrowed {
  explicit Borrowed(const T *ptr) : data_(ptr) {}
  const T *get() const { return data_; }

private:
  const T *data_;
};

template <typename T> struct DelayedCopy {
private:
  using Ptr = Borrowed<T>;
  boost::variant<Ptr, T> data_;

  static_assert(std::is_move_constructible<T>::value, "");
  static_assert(std::is_copy_constructible<T>::value, "");

public:
  DelayedCopy() = delete;

  DelayedCopy(const DelayedCopy &) = delete;
  DelayedCopy &operator=(const DelayedCopy &) = delete;

  DelayedCopy(DelayedCopy &&) = default;
  DelayedCopy &operator=(DelayedCopy &&) = default;

  DelayedCopy(T &&value) : data_(std::move(value)) {}
  DelayedCopy(const T &cref) : data_(Borrowed<T>(&cref)) {}

  const T &ref() const { return boost::apply_visitor(RefVisitor(), data_); }

  friend T take_ownership(DelayedCopy &&cow) {
    return boost::apply_visitor(TakeOwnershipVisitor(), cow.data_);
  }

private:
  struct RefVisitor : public boost::static_visitor<const T &> {
    const T &operator()(Borrowed<T> ptr) const { return *ptr.get(); }
    const T &operator()(const T &ref) const { return ref; }
  };

  struct TakeOwnershipVisitor : public boost::static_visitor<T> {
    T operator()(Borrowed<T> ptr) const { return T(*ptr.get()); }
    T operator()(T &ref) const { return T(std::move(ref)); }
  };
};

struct Bar {
  Noisy data_;

  auto fl() -> DelayedCopy<Noisy> { return Noisy(); }
  auto fm() -> DelayedCopy<Noisy> { return data_; }

  template <typename Fn> void cpsl(Fn fn) { fn(Noisy()); }
  template <typename Fn> void cpsm(Fn fn) { fn(data_); }
};

static void client_observes(const Noisy &) { std::puts(__func__); }
static void client_requires_ownership(Noisy) { std::puts(__func__); }

int main() {
  Bar a;

  std::puts("DelayedCopy:");
  auto afl = a.fl();
  auto afm = a.fm();

  client_observes(afl.ref());
  client_observes(afm.ref());

  client_requires_ownership(take_ownership(a.fl()));
  client_requires_ownership(take_ownership(a.fm()));

  std::puts("\nCPS:");

  a.cpsl(client_observes);
  a.cpsm(client_observes);

  a.cpsl(client_requires_ownership);
  a.cpsm(client_requires_ownership);
}

输出:

DelayedCopy:
Noisy: move ctor
client_observes
client_observes
Noisy: move ctor
Noisy: move ctor
client_requires_ownership
Noisy: copy ctor
client_requires_ownership

CPS:
client_observes
client_observes
client_requires_ownership
Noisy: copy ctor
client_requires_ownership

是否有更好的技术来传递避免额外拷贝但仍然通用的值(允许返回临时成员和数据成员)?

附带说明:代码是使用 C++11 中的 g++ 5.2 和 clang 3.7 编译的。在 C++14 和 C++1z 中,DelayedCopy 无法编译,我不确定这是否是我的错。

最佳答案

可能有成千上万种“正确”的方法。我会赞成一个:

  1. 传递引用或移动对象的方法已明确说明,因此没有人有任何疑问。
  2. 尽可能少地维护代码。
  3. 所有代码组合都能编译并执行明智的操作。

像这样的(人为的)例子:

#include <iostream>
#include <string>
#include <boost/optional.hpp>

// an object that produces (for example) strings
struct universal_producer
{
    void produce(std::string s)
    {
        _current = std::move(s);
        // perhaps signal clients that there is something to take here?
    }

    // allows a consumer to see the string but does not relinquish ownership
    const std::string& peek() const {
        // will throw an exception if there is nothing to take
        return _current.value();
    }

    // removes the string from the producer and hands it to the consumer
    std::string take() // not const
    {
        std::string result = std::move(_current.value());
        _current = boost::none;
        return result;
    }

    boost::optional<std::string> _current;

};

using namespace std;

// prints a string by reference
void say_reference(const std::string& s)
{
    cout << s << endl;
}

// prints a string after taking ownership or a copy depending on the call context
void say_copy(std::string s)
{
    cout << s << endl;
}

auto main() -> int
{
    universal_producer producer;
    producer.produce("Hello, World!");

    // print by reference
    say_reference(producer.peek());

    // print a copy but don't take ownership
    say_copy(producer.peek());

    // take ownership and print
    say_copy(producer.take());
    // producer now has no string. next peek or take will cause an exception
    try {
        say_reference(producer.peek());
    }
    catch(const std::exception& e)
    {
        cout << "exception: " << e.what() << endl;
    }
    return 0;
}

预期输出:

Hello, World!
Hello, World!
Hello, World!
exception: Attempted to access the value of an uninitialized optional object.

关于c++ - 如果方法的调用者不需要数据的所有权,有什么好的方法可以避免复制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32921608/

相关文章:

c++ - 如何在 LLVM IR 中获取字符串文字的值?

c++ - 流中奇怪的eof标志

c++ - 为什么即使成员函数是 constexpr 也需要 constexpr?

c++ - 迭代 std::set 如何返回排序结果

c++ - 没有公共(public)构造函数的类模板作为另一个类模板的成员

python - 从哪里获取适用于 Visual Studio 的 Python DLL?

c++ - 在同一连接或拆分连接上流式传输视频和命令?

c++ - 模板类型定义的新 "using"语法解决了什么问题?

C++ static_assert 和编译时

c++ - C++14 中的有符号位域