2

我想为任意函数生成包装器

R func(A a, B b, ...)

一个

std::function<std::string (std::vector<std::string>)>

被返回,它进行换f行,以便从向量中获取参数并(尝试)转换为boost::lexical_cast,与返回值相同。例如:

int add(int a, int b);

auto f = magic_wrap(&add);
auto result = f(std::vector<std::string>{"2", "3"});
// result == "5"

我的直觉告诉我这应该可以通过可变参数模板实现,但我一直坚持获取函数的返回和参数类型。这有可能以某种方式实现吗?如果可以,怎么做?

4

1 回答 1

5

我对此一无所知,boost::lexical_cast但我认为这应该可行:

template<std::size_t... Is>
struct index_sequence
{ };

template<std::size_t N, std::size_t... Is>
struct make_index_sequence : make_index_sequence<N - 1, N - 1, Is...>
{ };

template<std::size_t... Is>
struct make_index_sequence<0, Is...> : index_sequence<Is...>
{ };

template<typename R, typename... Args>
class wrapped
{
public:
    explicit
    wrapped(R (&func)(Args...))
        : func_(func)
    {
    }

public:
    std::string operator()(std::vector<std::string> args)
    {
        if (sizeof...(Args) != args.size()) {
            throw std::logic_error("Incorrect number of arguments");
        }

        auto const& result = invoke(make_index_sequence<sizeof...(Args)>(),
                                    args);
        return boost::lexical_cast<std::string>(result);
    }

private:
    template<std::size_t... Is>
    R invoke(index_sequence<Is...>, std::vector<std::string> const& args)
    {
        return func_(boost::lexical_cast<Args>(args[Is])...);
    }

private:
    R (*func_)(Args...);
};

template<typename R, typename... Args>
std::function<std::string (std::vector<std::string>)>
wrap(R (&func)(Args...))
{
    return wrapped<R, Args...>(func);
}

您可以在此处查看稍作修改的版本(不使用 Boost 的版本)的工作演示。

于 2013-10-09T15:15:59.420 回答