[英]Initializing array with results of variadic class template type's member function?
Say I have a template type...假设我有一个模板类型...
template<typename ...Ts>
struct SomeStruct {
std::tuple<Ts...> some_tuple;
}
... where I'm sure any type expanded from Ts
all share a base class, and thus some member function that returns some type R
. ...我确信从
Ts
扩展的任何类型都共享一个基本 class,因此某些成员 function 返回某些类型R
。
How could I go about collecting the results of calling that member function on each type in Ts
, for example in something like std::array< R, sizeof...(Ts) >
?我怎么能 go 关于在
Ts
中的每种类型上收集调用该成员 function 的结果,例如在std::array< R, sizeof...(Ts) >
类的东西中,
Given that all Ts...
element are supporting a foo()
method, returning a R
value, I suppose you can use std::apply()
, if you can use C++17鉴于所有
Ts...
元素都支持foo()
方法,返回R
值,我想你可以使用std::apply()
,如果你可以使用 C++17
std::apply([](auto ... vals)
{ return std::array<R, sizeof...(vals)>{ vals.foo()... }; },
some_tuple);
or maybe simply或者也许只是
std::apply([](auto ... vals)
{ return std::array { vals.foo()... }; },
some_tuple);
using std::array
deduction guides.使用
std::array
推导指南。
If you can use only C++14, you can emulate std::apply()
using std::make_index_sequence
/ std::index_sequence
如果只能使用 C++14,则可以使用
std::make_index_sequence
/ std::index_sequence
模拟std::apply()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.