Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C++;?_C++_Templates_C++11 - Fatal编程技术网

C++ 如何在C++;?

C++ 如何在C++;?,c++,templates,c++11,C++,Templates,C++11,我希望以这样的方式生成包装器,以便为任意函数 R func(A a, B b, ...) 一个 我的直觉告诉我,这在可变模板中应该是可能的,但我一直在获取函数的返回和参数类型。这是否可能,如果可能,如何实现?我对boost::lexical\u cast不太了解,但我认为这应该是可行的: template<std::size_t... Is> struct index_sequence { }; template<std::size_t N, std::size_t...

我希望以这样的方式生成包装器,以便为任意函数

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


我的直觉告诉我,这在可变模板中应该是可能的,但我一直在获取函数的返回和参数类型。这是否可能,如果可能,如何实现?

我对
boost::lexical\u 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);
}
模板
结构索引\u序列
{ };
模板
结构生成索引序列:生成索引序列
{ };
模板
结构生成索引序列:索引序列
{ };
模板
类包装
{
公众:
明确的
已包装(R&func)(参数…)
:func_(func)
{
}
公众:
std::string运算符()(std::vector args)
{
如果(sizeof…(Args)!=Args.size()){
抛出std::logic_错误(“参数数量不正确”);
}
auto const&result=invoke(生成索引序列(),
args);
返回boost::词法转换(结果);
}
私人:
模板
R调用(索引_序列,std::vector const和args)
{

return func_(boost::lexical_cast.

您无法将
std::vector
(其编译时长度未知)解包到参数列表中。函数参数列表不是列表;-)如果所有函数都将单个列表作为参数列表,您肯定可以简化工作。
int add(int a, int b);

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