gpt4 book ai didi

c++ - 如何获取指向 Factory::method(variadics ...) 的指针以进行延迟调用?

转载 作者:行者123 更新时间:2023-11-28 06:19:06 25 4
gpt4 key购买 nike

我从这个网站找到了下面的代码,但我无法适应我的目的:

我有用于创建带有可变参数的对象的工厂方法,并且有函数“apply”用于从工厂调用方法并解压到之前保存到 std::tuple 中的参数...

感谢您的帮助!

#include <iostream>
#include <utility>
#include <iostream>
#include <functional>
#include <utility>
#include <tuple>

class A {
public:
A(int, int){}
};

class B : public A {
public:
B(int x, int y):A(x, y){}
};

template <class Base, class Derived>
class BaseFactory {
public:
template <class ...Args>
static Base* create(Args&&... args){
return new Derived(std::forward<Args>(args)...);
}
};


// ------------- UTILITY---------------
template<int...> struct index_tuple{};

template<int I, typename IndexTuple, typename... Types>
struct make_indexes_impl;

template<int I, int... Indexes, typename T, typename ... Types>
struct make_indexes_impl<I, index_tuple<Indexes...>, T, Types...> {
typedef typename make_indexes_impl<I + 1, index_tuple<Indexes..., I>, Types...>::type type;
};

template<int I, int... Indexes>
struct make_indexes_impl<I, index_tuple<Indexes...> > {
typedef index_tuple<Indexes...> type;
};

template<typename ... Types>
struct make_indexes : make_indexes_impl<0, index_tuple<>, Types...> {};


// ----------UNPACK TUPLE AND APPLY TO FUNCTION ---------

template<class Ret, class... Args, int... Indexes >
Ret apply_helper( Ret (*pf)(Args...), index_tuple< Indexes... >, std::tuple<Args...>&& tup){
return pf( std::forward<Args>( std::get<Indexes>(tup))... );
}

template<class Ret, class ... Args>
Ret apply(Ret (*pf)(Args...), const std::tuple<Args...>& tup){
return apply_helper(pf, typename make_indexes<Args...>::type(), std::tuple<Args...>(tup));
}

template<class Ret, class ... Args>
Ret apply(Ret (*pf)(Args...), std::tuple<Args...>&& tup){
return apply_helper(pf, typename make_indexes<Args...>::type(), std::forward<std::tuple<Args...>>(tup));
}

int main(){
// I got error: apply(<unresolved overloaded function type>, std::tuple<int, int>)
A* = apply(&BaseFactory<A, B>::create<int,int>, std::make_tuple(5, 25));
return 0;
}

...是的,我确实读取了编译器的输出,但我如何才能获得代码的工作版本?

最佳答案

apply 的函数类型不正确。它应该是 Args&&...(就像您在 BaseFactory 中那样)而不是 Args...:

template<class Ret, class... Args, int... Indexes >
Ret apply_helper( Ret (*pf)(Args&&...), index_tuple< Indexes... >, std::tuple<Args...>&& tup){
// ^^
return pf( std::forward<Args>( std::get<Indexes>(tup))... );
}

template<class Ret, class ... Args>
Ret apply(Ret (*pf)(Args&&...), const std::tuple<Args...>& tup){
// ^^
return apply_helper(pf, typename make_indexes<Args...>::type(), std::tuple<Args...>(tup));
}

template<class Ret, class ... Args>
Ret apply(Ret (*pf)(Args&&...), std::tuple<Args...>&& tup){
// ^^
return apply_helper(pf, typename make_indexes<Args...>::type(), std::forward<std::tuple<Args...>>(tup));
}

int main(){
A* a = apply(&BaseFactory<A, B>::create<int,int>, std::make_tuple(5, 25));
return 0;
}

关于c++ - 如何获取指向 Factory::method(variadics ...) 的指针以进行延迟调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29617318/

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