我对此一无所知,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 的版本)的工作演示。