实施打字元组包装器
一个实现看起来如何,将例如 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的尝试固有的问题是类模板参数推导与它的函数对应物不同。如果显式指定类模板的任何参数,则不会进行任何推导。您会失败,因为尾随包始终指定(通过省略)为空。
解决方案是将负担转移到允许您仅指定部分参数的机制上:函数模板。
现在,表达式
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.
Now the type of the expression
data_of<a>(1, 2.0)
has the same "meta" properties you are after.