gpt4 book ai didi

c++ - 运行任何带有一半参数的仿函数

转载 作者:行者123 更新时间:2023-11-30 03:14:16 25 4
gpt4 key购买 nike

我想达到什么目的:我有以下类 Request,它有自己的模板化函数。我想用两种状态来调用它:
1) 提供参数;
2) 只有一个参数,其他参数应该作为默认值创建

template <typename TCmd> class Request{
public:
Request(TCmd applyingCommand): cmd(applyingCommand){}
template<typename ...TReplyData> void onSuccess(TReplyData... args){
// cmd(true, args...);
}
void onFail(){
// cmd(false) //here I want to create a wrapper, who calls the cmd with false + empty arguments
//which should be created by their constructor.
}
TCmd cmd;
};

这里是我想如何使用它:

auto lambda = [](bool isSucceed, int v, std::vector<uint> vec){
//doing smth
qDebug() << "insideLamda" << isSucceed << v << vec;
};
std::function<void(bool, int, std::vector<uint>)> fu = lambda;
Request req(fu);
req.onSuccess(4, std::vector<uint>{1,2});
req.onFail();

所以我的想法是如何实现它,但我坚持使用元组和可变参数模板重点是创建一个函数的后续包装器

template <typename ...Args> class CmdFu
{
public:
explicit CmdFu(std::function<void(Args...)> f): m_function(f){
}
template <typename ...ProvidedArgs>void call(ProvidedArgs... args){
m_function(args...);
}
template <typename ...ProvidedArgs>void callWithDefault(ProvidedArgs...args){ //here
auto neededIndecies = std::make_index_sequence<sizeof... (Args)>{};
size_t sizeOfRemainingIndecies = sizeof... (Args) - sizeof... (args);
callDefault(neededIndecies, args...);
}
private:
template<class T> T create(){
T t; return t;
}
template <typename ...ProvidedArgs, size_t...indecies> void callDefault( std::index_sequence<indecies...>, ProvidedArgs...args){
auto providedTuple = std::make_tuple(args...);
auto providedIndecies = std::index_sequence_for<ProvidedArgs...>();
//Approach #1: I create whole default tuple and somehow applying my provided Tuple to it
// NeededTuple t;
// ResultTuple r??
// std::apply(m_function, r);

//Aprroach #2: I make std::index_sequence with remaining indexes, like 2,3,4,5 and create remaining tuple
//then make the resulting tuple with std::tuple_cat
// also std::apply
}
size_t sizeOfNeededIndecies;
std::function<void(Args...)> m_function;
using NeededTuple = std::tuple<Args...>;
};

这是我的主要问题:
1) 如何将我自己的参数设置为默认元组?
2) 如何创建起始 sizeOfRemainingIndecies 的 index_sequence ?
3) 是否可以检查调用签名以避免在 call() 内运行时崩溃?

最佳答案

如果你不包装在 std::function 中,但保留仿函数,你可能已经这样做了

auto lambda = [](bool isSucceed, int v = 0, std::vector<uint> vec = {}){
//doing smth
qDebug() << "insideLamda" << isSucceed << v << vec;
};
Request req(lambda);
req.onSuccess(4, std::vector<uint>{1,2});
req.onFail();

Demo

对于 CmdFu 的实现,你可以这样做:

template <typename ...Args> class CmdFu
{
public:
explicit CmdFu(std::function<void(Args...)> f): m_function(f){}

template <typename ...Ts>
void call(Ts&&... args){
m_function(std::forward<Ts>(args)...);
}

template <typename ...Ts>
void callWithDefault(Ts&&...args)
{
callDefaultImpl<sizeof...(Ts)>(std::make_index_sequence<sizeof...(Args)
- sizeof...(Ts)>{},
std::forward<Ts>(args)...);
}
private:

template <std::size_t Offset, size_t...Is, typename ...Ts>
void callDefaultImpl(std::index_sequence<Is...>, Ts&&...args){
m_function(std::forward<Ts>(args)...,
std::tuple_element_t<Offset + Is, std::tuple<Args...>>{}...);
}
std::function<void(Args...)> m_function;
};

Demo

1) How to set my own arguments to default tuple?

创建默认缺少的参数是通过以下方式完成的:

std::tuple_element_t<Offset + Is, std::tuple<Args...>>{}...

2) How to create index_sequence with starting sizeOfRemainingIndecies ?

您可以创建常规索引序列和偏移量:

template <std::size_t Offset, std::size_t... Is>
constexpr std::index_sequence<(Offset + Is)...>
index_sequence_with_offset(std::index_sequence<Is...>)
{ return {}; }

3) Is it possible to check the calling signature to avoid runtime crashes inside call()?

如果签名不匹配,它将无法编译,如果需要,您可以使用 SFINAE 丢弃无效的重载。有几种可能的语法,例如:

template <typename ...Ts>
auto call(Ts&&... args)
-> decltype(m_function(std::forward<Ts>(args)...), void())
{
m_function(std::forward<Ts>(args)...);
}

关于c++ - 运行任何带有一半参数的仿函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57991245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com