简体   繁体   English

使用可变参数 class 模板类型的成员 function 的结果初始化数组?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM