将可变包类型转换为值? [英] Transform a variadic pack of types into values?

查看:152
本文介绍了将可变包类型转换为值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些功能

template<typename T>
constexpr Foo make_foo<T>();

这实质上将类型映射到 Foo 没有副作用。

which essentially maps types to instances of Foo with no side-effects.

现在,我想写函数 magic

template<typename Types...>
vector<Foo> magic();

与make_foo相同,并以一种方式,它会是那么容易我说,流所有这些Foo的std :: cout,或在循环中迭代它们。我意识到这个问题不是完全明确定义,因为我'不清楚如果我正在寻找的相关输出是一些可变变量包的值(看到如何在运行时不存在)。

which does the same as make_foo, but for variadic parameter packs; and in a way which it would then be easy for me to, say, stream all these Foo's to std::cout, or to iterate over them in a loop etc. I realize this question is not entirely well-defined, since I'm not clear if the relevant output I'm looking for is somekind of variadic packs of values (seeing how that doesn't exist in runtime).

那么,什么是惯用的做法?我注意到Eric Niebler有一个关于元编程库的博客页面似乎是相关的,但它似乎有点过分。对我的情况。

So, what's the idiomatic way of doing that? I noticed that Eric Niebler has a blog page about a metaprogramming library which seems to be relevant, but it seems like a bit overkill. For my case.

推荐答案

如果你需要一个 Foo ,则只需

If you need a vector of Foos, then it's just

template<typename Types...>
vector<Foo> magic(){
    return { make_foo<Types>()... };
}

您也可以将它们放入本地数组, :

You can also throw them into a local array and play with them however you want:

Foo foos[] = { make_foo<Types>()... };

这篇关于将可变包类型转换为值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆