c++ - 如何为 std::tuple 包装器类启用结构化绑定(bind)?

标签 c++ tuples c++17 structured-bindings

我正在尝试为另一个具有私有(private) std::tuple 成员的类实现包装类,并在包装​​类上启用结构化绑定(bind)。这是带有私有(private)元组的类:

class widget {
  friend class wrap;
  std::tuple<int, double> m_tuple {1, 1.0};
};

这是我在阅读有关如何为自定义类型启用结构化绑定(bind)(例如 this post on devblogs.microsoft.com)后对包装类的尝试:

class wrap {
public:
  wrap(widget& f) : m_tuple(f.m_tuple) {}

  // auto some_other_function();

  template<std::size_t Index>
  auto get() & -> std::tuple_element_t<Index, wrap>& {
    return std::get<Index>(m_tuple);
  }

  template<std::size_t Index>
  auto get() && -> std::tuple_element_t<Index, wrap>& {
    return std::get<Index>(m_tuple);
  }

  template<std::size_t Index>
  auto get() const& -> const std::tuple_element_t<Index, wrap>& {
    return std::get<Index>(m_tuple);
  }

  template<std::size_t Index>
  auto get() const&& -> const std::tuple_element_t<Index, wrap>& {
    return std::get<Index>(m_tuple);
  }

private:
  std::tuple<int, double>& m_tuple;
};

这里是wrap的专用std::tuple_sizestd::tuple_element:

namespace std {

template<>
struct tuple_size<wrap> : tuple_size<std::tuple<int, double>> {};

template<size_t Index>
struct tuple_element<Index, wrap> : tuple_element<Index, tuple<int, double>> {};
  
}  // namespace std

我想要以下行为:

int main() {
  widget w;

  auto [i_copy, d_copy] = wrap(w);

  i_copy = 2;    // Does not change w.m_tuple because i_copy is a copy of std::get<0>(w.m_tuple).
  d_copy = 2.0;  // Does not change w.m_tuple because d_copy is a copy of std::get<1>(w.m_tuple).

  // w.m_tuple still holds {1, 1.0}.

  auto& [i_ref, d_ref] = wrap(w);

  i_ref = 2;   // Changes w.m_tuple because i_ref is a reference to std::get<0>(w.m_tuple).
  d_ref = 2.0; // Changes w.m_tuple because d_ref is a reference to std::get<1>(w.m_tuple).

  // w.m_tuple now holds {2, 2.0}.
}

但这甚至无法编译(使用 gcc 12.2.0 和 clang 14.0.6 测试)。我得到的错误是

error: cannot bind non-const lvalue reference of type ‘wrap&’ to an rvalue of type ‘wrap’
    |   auto& [i_ref, d_ref] = wrap(w);

'wrap&' 类型的非 const 左值引用 是否引用 auto& [i_ref, d_ref]'wrap' 类型的右值 code> 到 wrap(w)?为什么 i_refd_ref 不是对 w 中元组的整数和 double 的引用?

编辑:如何实现具有所需行为的包装器类?

最佳答案

在你的第二个例子中你有

auto& [i_ref, d_ref] = wrap(w);

auto& 是绑定(bind)到 = 右侧表达式的不可见变量的类型,它是一个临时变量。就像在做

wrap& wr = wrap(w);

我学到了这个from T.C. in 2016 ,请参阅所有相关引文的答案。

We can produce the same error without any of your classes ,只是一个元组和一个 auto&

这段代码:

auto& [i, d] = std::tuple{1, 1.0};

产生这个错误:

prog.cc: In function 'int main()':
prog.cc:4:25: error: cannot bind non-const lvalue reference of type 'std::tuple<int, double>&' to an rvalue of type 'std::tuple<int, double>'
    4 |     auto& [i, d] = std::tuple{1, 1.0};
      |                         ^~~~~~~~~~~~~

您不能将非常量左值引用绑定(bind)到临时引用。如果您将行更改为

const auto& [i_ref, d_ref] = wrap(w);

然后你通过那一行,但不能使用 i_refd_ref 修改任何东西,因为它们现在是 const 对象的字段的别名。如果您想要可以修改的内容,则必须为 wrap 对象命名。

auto wrapped = wrap(w);
auto& [i_ref, d_ref] = wrapped;
i_ref = 2;
assert(std::get<0>(w.m_tuple) == 2);

live here

您也可以使用右值引用来完成这项工作

auto&& [i_ref, d_ref] = wrap(w);
i_ref = 2;
assert(std::get<0>(w.m_tuple) == 2);

live here

或者根本没有引用

auto [i_ref, d_ref] = wrap(w);
i_ref = 2;
assert(std::get<0>(w.m_tuple) == 2);

关于c++ - 如何为 std::tuple 包装器类启用结构化绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74594151/

相关文章:

c++ - 如何调用带参数的类模板函数

c++覆盖数组类的>>运算符

c++ - 如何创建 std::tuple 的别名?

python - 字符串索引超出范围Python元组

c++ - Eclipse CDT 将 static_assert(cond) 突出显示为语法错误

python - 在 C++ 中循环 "all the way through"数组是否有最佳实践?

c++ - gdb - 确定段错误的原因

python - 将元组转换为列表并返回

c++ - 不同类别的 vector

c++ - 通过引用重载多个函数对象