将元组和整数实例合并到引用元组中
Merge tuple and integral instances into tuple of references
我在尝试从元组和整数值的混合构造引用元组时遇到了奇怪的行为。
鉴于以下情况:
struct A { int v = 1; };
struct B { int v = 2; };
struct C { int v = 3; };
A a;
std::tuple<B,C> tpl;
我正在尝试创建第三个元组,它包含对所有实例的引用,以便每个实例的 v
都可以通过它分配和读取。
使用模板似乎很简单
template <class Tuple, size_t... Is>
constexpr auto as_ref_impl(Tuple t, std::index_sequence<Is...>) {
return std::tuple_cat(std::tie(std::get<Is>(t))...);
// or
// return std::make_tuple(std::ref(std::get<Is>(t))...);
}
template <class...Args>
constexpr auto as_ref(std::tuple<Args...>& t) {
return as_ref_impl(t, std::index_sequence_for<Args...>{});
}
然后
auto ref_tpl = std::tuple_cat(std::tie(a), as_ref(tpl));
构建良好(在两个版本中)。
遗憾的是,只有源自整数值的引用元组 (ref_tpl
) 的部分可以成功分配或读取。
我正在使用 C++14
和 gcc 9.3.0
。
非常欢迎任何想法或洞察为什么这不起作用!
最小工作示例:
#include <iostream>
#include <tuple>
#include <type_traits>
#include <utility>
#include <functional>
struct A { int v = 1; };
struct B { int v = 2; };
struct C { int v = 3; };
A a;
std::tuple<B,C> tpl;
template <class Tuple, size_t... Is>
constexpr auto as_ref_impl(Tuple t, std::index_sequence<Is...>) {
//return std::tuple_cat(std::tie(std::get<Is>(t))...);
return std::make_tuple(std::ref(std::get<Is>(t))...);
}
template <class...Args>
constexpr auto as_ref(std::tuple<Args...>& t) {
return as_ref_impl(t, std::index_sequence_for<Args...>{});
}
int main() {
using std::cout;
auto ref_tpl = std::tuple_cat(std::tie(a), as_ref(tpl));
// prints 1 2 3, as expected.
cout << a.v << std::get<0>(tpl).v << std::get<1>(tpl).v << std::endl;
std::get<0>(ref_tpl).v = 8; // works
std::get<1>(ref_tpl).v = 9; // does not work
std::get<2>(ref_tpl).v = 10; // does not work
// should output 8 9 10 instead outputs 8 2 3
cout << a.v << std::get<0>(tpl).v << std::get<1>(tpl).v << std::endl;
// should output 8 9 10, instead outputs garbage.
cout << std::get<0>(ref_tpl).v << std::get<1>(ref_tpl).v << std::get<2>(ref_tpl).v << std::endl;
return 0;
}
这是一个简单的错字:
constexpr auto as_ref_impl(Tuple t, std::index_sequence<Is...>) {
Tuple
是按值取的,所以做了一个本地拷贝,引用是相对的
您应该参考 Tuple
,
constexpr auto as_ref_impl(Tuple& t, std::index_sequence<Is...>) {
您的 as_ref_impl
需要通过引用获取 Tuple
参数,否则您将 std::ref
用于局部函数。这解释了 tpl
的未修改值和 ref_tpl
.
中的垃圾值
改为这样做:
template <class Tuple, size_t... Is>
// note the reference parameter
constexpr auto as_ref_impl(Tuple &t, std::index_sequence<Is...>) {
return std::make_tuple(std::ref(std::get<Is>(t))...);
}
这是 demo。
我在尝试从元组和整数值的混合构造引用元组时遇到了奇怪的行为。
鉴于以下情况:
struct A { int v = 1; };
struct B { int v = 2; };
struct C { int v = 3; };
A a;
std::tuple<B,C> tpl;
我正在尝试创建第三个元组,它包含对所有实例的引用,以便每个实例的 v
都可以通过它分配和读取。
使用模板似乎很简单
template <class Tuple, size_t... Is>
constexpr auto as_ref_impl(Tuple t, std::index_sequence<Is...>) {
return std::tuple_cat(std::tie(std::get<Is>(t))...);
// or
// return std::make_tuple(std::ref(std::get<Is>(t))...);
}
template <class...Args>
constexpr auto as_ref(std::tuple<Args...>& t) {
return as_ref_impl(t, std::index_sequence_for<Args...>{});
}
然后
auto ref_tpl = std::tuple_cat(std::tie(a), as_ref(tpl));
构建良好(在两个版本中)。
遗憾的是,只有源自整数值的引用元组 (ref_tpl
) 的部分可以成功分配或读取。
我正在使用 C++14
和 gcc 9.3.0
。
非常欢迎任何想法或洞察为什么这不起作用!
最小工作示例:
#include <iostream>
#include <tuple>
#include <type_traits>
#include <utility>
#include <functional>
struct A { int v = 1; };
struct B { int v = 2; };
struct C { int v = 3; };
A a;
std::tuple<B,C> tpl;
template <class Tuple, size_t... Is>
constexpr auto as_ref_impl(Tuple t, std::index_sequence<Is...>) {
//return std::tuple_cat(std::tie(std::get<Is>(t))...);
return std::make_tuple(std::ref(std::get<Is>(t))...);
}
template <class...Args>
constexpr auto as_ref(std::tuple<Args...>& t) {
return as_ref_impl(t, std::index_sequence_for<Args...>{});
}
int main() {
using std::cout;
auto ref_tpl = std::tuple_cat(std::tie(a), as_ref(tpl));
// prints 1 2 3, as expected.
cout << a.v << std::get<0>(tpl).v << std::get<1>(tpl).v << std::endl;
std::get<0>(ref_tpl).v = 8; // works
std::get<1>(ref_tpl).v = 9; // does not work
std::get<2>(ref_tpl).v = 10; // does not work
// should output 8 9 10 instead outputs 8 2 3
cout << a.v << std::get<0>(tpl).v << std::get<1>(tpl).v << std::endl;
// should output 8 9 10, instead outputs garbage.
cout << std::get<0>(ref_tpl).v << std::get<1>(ref_tpl).v << std::get<2>(ref_tpl).v << std::endl;
return 0;
}
这是一个简单的错字:
constexpr auto as_ref_impl(Tuple t, std::index_sequence<Is...>) {
Tuple
是按值取的,所以做了一个本地拷贝,引用是相对的
您应该参考 Tuple
,
constexpr auto as_ref_impl(Tuple& t, std::index_sequence<Is...>) {
您的 as_ref_impl
需要通过引用获取 Tuple
参数,否则您将 std::ref
用于局部函数。这解释了 tpl
的未修改值和 ref_tpl
.
改为这样做:
template <class Tuple, size_t... Is>
// note the reference parameter
constexpr auto as_ref_impl(Tuple &t, std::index_sequence<Is...>) {
return std::make_tuple(std::ref(std::get<Is>(t))...);
}
这是 demo。