通过通过另一个std的类型获取std ::元组的元素

发布于 2025-01-23 13:54:05 字数 2237 浏览 0 评论 0原文

我的结构使管理元组变得更容易:

template<class... Ts>
struct Store
{
    using base_type = std::tuple<Ts...>;

    base_type store;

    Store(Ts... args)
    {
        store = {args...};
    }

    template<size_t... Ints>
    decltype(auto) Get()
    {
        if constexpr(sizeof...(Ints) == 1)
            return std::get<Ints...>(store);
        else
            return std::make_tuple(std::get<Ints>(store)...);
    }

    template<class... Us>
    decltype(auto) Get()
    {
        if constexpr(sizeof...(Us) == 1)
            return std::get<Us...>(store);
        else
            return std::make_tuple(std::get<Us>(store)...);
    }
};

我还具有使我可以组合模板参数的结构:

template <typename... Ts>
struct store_helper
{ using type = Store<Ts...>; };

template <typename... Ts>
struct store_helper<Store<Ts...>>
{ using type = Store<Ts...>; };

template<typename ... Ts1, typename... Ts2>
struct store_helper<Store<Ts1...>, Ts2...>
: public store_helper<Store<Ts1..., Ts2...>>
{ };

template<typename ... Ts1, typename ... Ts2, typename... Ts3>
struct store_helper<Store<Ts1...>, Store<Ts2...>, Ts3...>
: public store_helper<Store<Ts1..., Ts2...>, Ts3...>
{ };

template<typename... Ts>
using Scheme = typename store_helper<Ts...>::type;

现在我可以做到这一点:

int main() {

    using Vector2D = Scheme<int, int>;

    using Position = Scheme<Vector2D>;
    using Direction = Scheme<Vector2D>;

    using Player = Scheme<Position, Direction, float>;

    Player store(33, 44, 12, 122, 4.0);

    auto float_num = store.Get<float>();

    std::cout << float_num << std::endl;
    
    return 0;
}

但是我也想这样做:

    auto [x, y] = store.Get<Direction>();

    std::cout << x << std::endl;
    std::cout << y << std::endl;

我需要通过通过元组来检索数据他创造了它。有什么方法可以实施此语法?

I have a structure that makes it easier to manage the tuple:

template<class... Ts>
struct Store
{
    using base_type = std::tuple<Ts...>;

    base_type store;

    Store(Ts... args)
    {
        store = {args...};
    }

    template<size_t... Ints>
    decltype(auto) Get()
    {
        if constexpr(sizeof...(Ints) == 1)
            return std::get<Ints...>(store);
        else
            return std::make_tuple(std::get<Ints>(store)...);
    }

    template<class... Us>
    decltype(auto) Get()
    {
        if constexpr(sizeof...(Us) == 1)
            return std::get<Us...>(store);
        else
            return std::make_tuple(std::get<Us>(store)...);
    }
};

I also have structures that allow me to combine template arguments:

template <typename... Ts>
struct store_helper
{ using type = Store<Ts...>; };

template <typename... Ts>
struct store_helper<Store<Ts...>>
{ using type = Store<Ts...>; };

template<typename ... Ts1, typename... Ts2>
struct store_helper<Store<Ts1...>, Ts2...>
: public store_helper<Store<Ts1..., Ts2...>>
{ };

template<typename ... Ts1, typename ... Ts2, typename... Ts3>
struct store_helper<Store<Ts1...>, Store<Ts2...>, Ts3...>
: public store_helper<Store<Ts1..., Ts2...>, Ts3...>
{ };

template<typename... Ts>
using Scheme = typename store_helper<Ts...>::type;

Now I can do this:

int main() {

    using Vector2D = Scheme<int, int>;

    using Position = Scheme<Vector2D>;
    using Direction = Scheme<Vector2D>;

    using Player = Scheme<Position, Direction, float>;

    Player store(33, 44, 12, 122, 4.0);

    auto float_num = store.Get<float>();

    std::cout << float_num << std::endl;
    
    return 0;
}

But I'd also like to do that:

    auto [x, y] = store.Get<Direction>();

    std::cout << x << std::endl;
    std::cout << y << std::endl;

I need to retrieve data by passing the tuple from which it he created. Is there any way to implement this syntax ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文