Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

c++ - Cross multiply non-type variadic templates

My goal is to make this template work:

template <size_t... Ns>
struct mult
{
    using cross = ?; // variadic size_t
    static_assert(sizeof...(cross) + 1 == sizeof...(Ns), "");
};

So I can use it like this:

mult<2,3,5>::cross // 6,15 // because 2*3=6, 3*5=15
mult<3,5,7,11>::cross // 15,35,77 // because 3*5=15, 5*7=35, 7*11=77

Because I need to make this:

// tuple of arrays
std::tuple<std::array<size_t, mult<Ns...>::cross>...> cross_arrays;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want compile time computation, write constexpr functions

template<typename... SizeT>
constexpr auto cross(size_t x, SizeT... xs)
{
    std::array tmp = {x, xs...};
    std::array ret = {xs...};
    for(size_t i = 0; i < ret.size(); i++)
        ret[i] *= tmp[i];
    return ret;
}

template<size_t... xs, size_t... Is>
auto cross_tuple_fn(std::index_sequence<Is...>)
{
    constexpr auto dims = cross(xs...);
    return std::tuple<std::array<size_t, dims[Is]>...>{};
}

template<size_t... xs>
auto cross_tuple_fn()
{
    return cross_tuple_fn<xs...>(std::make_index_sequence<sizeof...(xs) - 1>{});
}

template<size_t... xs>
using cross_tuple_t = decltype(cross_tuple_fn<xs...>());

They're easier to read, write and looks more or less the same as normal functions.

Use as

cross_tuple_t<Ns...> cross_arrays;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...