为以后知道返回类型的函数存储参数包

问题描述 投票:0回答:1

如何存储一个参数包,该参数包将作为参数传递给函数(稍后而不是立即发送,因此需要位于某个成员/ var中),稍后再调用?

例如

class foo
{
    void* fn;
    magic_type x;
public:
    template<typename... t> set_parameters(t... params){ x = params }
    set_function_ptr(void*fn){ this->fn = fn; }
    template<typename t> call( ){ return std::function< t( x.get_param_types... ) >( fn )( x... );
};

[magic_type可以满足此要求而保持以下用法,而foo本身不依赖于模板参数吗?std::tuple/ forward_as_tuple? lambdas? std::any

foo bar;
bar.set_parameters( 2.f, 2.f );
bar.set_function_ptr( powf );
auto fl = bar.call< float >( );

我确定我提到的所有内容都可以以[[some形式使用,但是我想要最好/最推荐的方法,除非更改用法。

c++ lambda tuples any
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.