实施打字元组包装器

发布于 2025-01-20 18:45:54 字数 1108 浏览 5 评论 0原文

一个实现看起来如何,将例如 std::tuple 包装为类型/值的静态列表,加上一个类型(不包含在元组中)来引用某种所有者/访问者。

我想实例化像

constexpr auto data = data_of(1, 2.0);

Background

这个想法是使用 data_of(.. .) 一种将类型/数据对列表传递到批量函数中的结构,如下所示。

template <typename... _Ts>
static constexpr void do_bulk_stuff(_Ts&&... _Vs)
{
    // crazy stuff happens here
}
// call it like
do_bulk_stuff
(
    data_of<a>(1, 2.0), 
    data_of<b>(3), 
    data_of<c>(4.0, 5, 6),
    // ...
);

尝试 1

这样幼稚(不起作用)的实现尝试,

template <typename T, typename... Ts>
struct data_of {
    using type = T;
    using data_t = std::tuple<Ts...>;
    data_t data;
    
    constexpr data_of(Ts&&... Vs)
    : data(Vs...)
    {}
};

到目前为止,我最终得到了像目标

我的目标是实现这样的结果 data_of实例伪代码示例

{
    // meta
    type = a;
    data_t = std::tuple<int,double>;

    // runtime
    data = [1, 2.0];
}

How can an implementation look like, that wraps around e.g. a std::tuple as a static list of type/value, plus a type (not contained in tuple) to refer to some kind of owner/visitor.

I want to instantiate like

constexpr auto data = data_of<a>(1, 2.0);

Background

The idea is to use a data_of<T>(...) kind of structure to to pass a list of type/data pairs into a bulk function like this.

template <typename... _Ts>
static constexpr void do_bulk_stuff(_Ts&&... _Vs)
{
    // crazy stuff happens here
}
// call it like
do_bulk_stuff
(
    data_of<a>(1, 2.0), 
    data_of<b>(3), 
    data_of<c>(4.0, 5, 6),
    // ...
);

Attempt 1

So far I ended up with a naive (not working) implementation attempt like

template <typename T, typename... Ts>
struct data_of {
    using type = T;
    using data_t = std::tuple<Ts...>;
    data_t data;
    
    constexpr data_of(Ts&&... Vs)
    : data(Vs...)
    {}
};

Goal

My goal is to achieve a result like this data_of<a> instance pseudo code example

{
    // meta
    type = a;
    data_t = std::tuple<int,double>;

    // runtime
    data = [1, 2.0];
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

像你 2025-01-27 18:45:54

您的尝试固有的问题是类模板参数推导与它的函数对应物不同。如果显式指定类模板的任何参数,则不会进行任何推导。您会失败,因为尾随包始终指定(通过省略)为空。

解决方案是将负担转移到允许您仅指定部分参数的机制上:函数模板。

template <typename T, typename... Ts>
struct data_of_t {
    using type = T;
    using data_t = std::tuple<Ts...>;
    data_t data;
    
    constexpr data_of_t(Ts&&... vs)
    : data(std::forward<Ts>(vs)...)
    {}
};

template<typename T, typename... Ts>
constexpr auto data_of(Ts&&... vs) {
    return data_of_t<T, Ts...>(std::forward<Ts>(vs)...);
}

现在,表达式 data_of(1, 2.0) 的类型具有与您所追求的相同的“元”属性。

The issue inherent to your attempt is that class template argument deduction is simply not like its function counterpart. There will be no deduction if any of the class template's arguments is explicitly specified. You fail because the trailing pack is always specified (by omission) as empty.

The solution is to shift the burden onto the mechanism that allows you to specify only part of the arguments: function templates.

template <typename T, typename... Ts>
struct data_of_t {
    using type = T;
    using data_t = std::tuple<Ts...>;
    data_t data;
    
    constexpr data_of_t(Ts&&... vs)
    : data(std::forward<Ts>(vs)...)
    {}
};

template<typename T, typename... Ts>
constexpr auto data_of(Ts&&... vs) {
    return data_of_t<T, Ts...>(std::forward<Ts>(vs)...);
}

Now the type of the expression data_of<a>(1, 2.0) has the same "meta" properties you are after.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文