在可变参数模板上生成变量列表

Generate a list of variables on variadic template

在 C++ 17 及更高版本中,可以将结构分解为其变量:

struct MyStruct
{
    int f1;
    int f2;
    int f3;
};
auto& [f1, f2, f3] = my_struct;

我正尝试在可变参数包函数中这样做:

template <class ... T, std::size_t...I>
constexpr std::size_t CountFields(std::index_sequence<I...>)
{
    T t;
    auto& [] = t;
    return 0;
}

但我不完全确定如何填充括号之间的 space 以获取变量。最终目标是使用这些变量来创建一个元组(即我正在尝试为元组函数创建一个通用结构)。我发现的最接近的是:.

但出于几个原因我需要避免使用 BOOST。

遗憾的是,显然无法在结构化绑定中使用参数包。另一方面,我们不能在没有结构化绑定的情况下绑定数据成员。

但是,如果你给出数据成员的数量,你可以通过传统方式绑定它:

template<size_t>
struct structToTupleHelper;

template<>
struct structToTupleHelper<0>; // ISO C++17 does not allow a decomposition group to be empty.

template<>
struct structToTupleHelper<1>{
    template<typename X, size_t... Is>
    static auto convert(X&& x, std::index_sequence<Is...>){
        auto&& [a1] = std::forward<X>(x); // bound variables are always thought as lvalue.
        auto temp = std::forward_as_tuple(a1);
        return std::forward_as_tuple(std::get<Is>(temp)...);
    }
};

template<>
struct structToTupleHelper<2>{
    template<typename X, size_t... Is>
    static auto convert(X&& x, std::index_sequence<Is...>){
        auto&& [a1, a2] = std::forward<X>(x); // bound variables are always thought as lvalue.
        auto temp = std::forward_as_tuple(a1, a2);
        return std::forward_as_tuple(std::get<Is>(temp)...);
    }
};

// ...
// maybe 16 is enough?

template<typename X, size_t... Is>
auto structToTuple(X&& x, std::index_sequence<Is...> _1){
    return structToTupleHelper<sizeof...(Is)>::convert(std::forward<X>(x), _1);
}

template<size_t N, typename X, size_t... Is>
auto structToTuple(X&& x, std::index_sequence<Is...> _1){
    return structToTupleHelper<N>::convert(std::forward<X>(x), _1);
}

template<size_t N, typename X>
auto structToTuple(X&& x){
    return structToTupleHelper<N>::convert(std::forward<X>(x), std::make_index_sequence<N>());
}

然后您可以使用它,例如:

int ii;
struct A{
    int& a;
} a{ii};
struct B{
    int a;
} b;

auto t1 = structToTuple<1>(A{ii}); // tuple<int&>, valid.
auto t2 = structToTuple<1>(a);     // tuple<int&>, valid.
auto t3 = structToTuple<1>(B{});   // tuple<int&>, invalid: a lvalue reference is bound to a temporary object.
auto t4 = structToTuple<1>(b);     // tuple<int&>, valid.
somefunc(structToTuple<1>(B{})); // valid. the temporary object is alive inside 'somefunc'.
structToTuple<1>(A{ii}) = std::tuple(1); // valid. assign 1 to 'ii'.
structToTuple<1>(a) = std::tuple(1);     // valid. assign 1 to 'ii'.
structToTuple<1>(B{}) = std::tuple(1);   // unexpected. assign 1 to the member of a temporary object.
structToTuple<1>(b) = std::tuple(1);     // valid. assign 1 to 'b.a'.

// struct C{
//     int a : 8;
// } c;
// auto t5 = structToTuple<1>(C{}); // invalid: bitfields can not be treated as non-const lvalue reference
// auto t6 = structToTuple<1>(c);   // invalid: bitfields can not be treated as non-const lvalue reference

可能你觉得做专科很麻烦。幸运的是,我们可以使用宏来简化它:(这正是 BOOST 一直在做的。)

#define XXX_CONCAT_HELPER(a, b) a##b
#define XXX_CONCAT(a, b) XXX_CONCAT_HELPER(a, b)

#define XXX_COMMA ,
#define XXX_COMMA_FUNC(a) ,
#define XXX_EMPTY
#define XXX_EMPTY_FUNC(a)

#define XXX_REPEAT_0(func, join)
#define XXX_REPEAT_1(func, join) func(1)
#define XXX_REPEAT_2(func, join) XXX_REPEAT_1(func,join) join(2) func(2)
// ...
#define XXX_REPEAT_256(func, join) XXX_REPEAT_255(func,join) join(256) func(256)

#define XXX_REPEAT(func, times, join) XXX_CONCAT(XXX_REPEAT_,times)(func,join)

// macro is not allowed to be recursive, so we need another repeat function.
#define XXX_ALIAS_REPEAT_0(func, join)
#define XXX_ALIAS_REPEAT_1(func, join) func(1)
#define XXX_ALIAS_REPEAT_2(func, join) XXX_ALIAS_REPEAT_1(func,join) join(2) func(2)
// ...
#define XXX_ALIAS_REPEAT_256(func, join) XXX_ALIAS_REPEAT_255(func,join) join(256) func(256)

#define XXX_ALIAS_REPEAT(func, times, join) XXX_CONCAT(XXX_ALIAS_REPEAT_,times)(func,join)

#define STRUCT_TO_TUPLE_TOKEN_FUNC(n) XXX_CONCAT(a,n)

#define STRUCT_TO_TUPLE_FUNC(n) \
template<> \
struct structToTupleHelper<n>{ \
    template<typename X, size_t... Is> \
    static auto convert(X&& x, std::index_sequence<Is...>){ \
        auto&& [XXX_REPEAT(STRUCT_TO_TUPLE_TOKEN_FUNC,n,XXX_COMMA_FUNC)] = std::forward<X>(x); \
        auto temp = std::forward_as_tuple(XXX_REPEAT(STRUCT_TO_TUPLE_TOKEN_FUNC,n,XXX_COMMA_FUNC)); \
        return std::forward_as_tuple(std::get<Is>(temp)...); \
    } \
}; \


XXX_ALIAS_REPEAT(STRUCT_TO_TUPLE_FUNC,128,XXX_EMPTY_FUNC)

#undef STRUCT_TO_TUPLE_TOKEN_FUNC
#undef STRUCT_TO_TUPLE_FUNC