c++ - 使用可变模板参数调用零参数模板函数指针?

标签 c++ c++11 variadic-templates template-function

这是来自 Functional C++ 的帖子的代码片段博客,描述了如何实现广义函数评估。

我的问题是你如何声明模板函数指针 f 像 R(C::*f)() 一样没有参数并且仍然能够用 Args 调用它......?

// functions, functors, lambdas, etc.
template<
    class F, class... Args,
    class = typename std::enable_if<!std::is_member_function_pointer<F>::value>::type,
    class = typename std::enable_if<!std::is_member_object_pointer<F>::value>::type
    >
auto eval(F&& f, Args&&... args) -> decltype(f(std::forward<Args>(args)...))
{
    return f(std::forward<Args>(args)...);
}

// const member function
template<class R, class C, class... Args>
auto eval(R(C::*f)() const, const C& c, Args&&... args) -> R
{
    return (c.*f)(std::forward<Args>(args)...);
}

template<class R, class C, class... Args>
auto eval(R(C::*f)() const, C& c, Args&&... args) -> R
{
    return (c.*f)(std::forward<Args>(args)...);
}

// non-const member function
template<class R, class C, class... Args>
auto eval(R(C::*f)(), C& c, Args&&... args) -> R
{
    return (c.*f)(std::forward<Args>(args)...);
}

// member object
template<class R, class C>
auto eval(R(C::*m), const C& c) -> const R&
{
    return c.*m;
}

template<class R, class C>
auto eval(R(C::*m), C& c) -> R&
{
    return c.*m;
}

struct Bloop
{
    int a = 10;
    int operator()(){return a;}
    int operator()(int n){return a+n;}
    int triple(){return a*3;}
};

int add_one(int n)
{
    return n+1;
}

int main()
{
    Bloop bloop;

    // free function
    std::cout << eval(add_one,0) << "\n";

    // lambda function
    std::cout << eval([](int n){return n+1;},1) << "\n";

    // functor
    std::cout << eval(bloop) << "\n";
    std::cout << eval(bloop,4) << "\n";

    // member function
    std::cout << eval(&Bloop::triple,bloop) << "\n";

    // member object
    eval(&Bloop::a,bloop)++; // increment a by reference
    std::cout << eval(&Bloop::a,bloop) << "\n";

    return 0;
}

例如,当我尝试:

struct Bloop
{
    int a = 10;
    int operator()(){return a;}
    int operator()(int n){return a+n;}
    int triple(){return a*3;}
    int foo(int n) {return n;}
};

template <typename R, typename C, typename... Args>
void eval (R(C::*func)(), C& c, Args... args) {
    (c.*func)(args...);
}

int main()
{
    Bloop bloop;

    eval(&Bloop::foo, bloop, 5);

    return 0;
}

我收到这个错误:

main.cpp: In function 'int main()':
main.cpp:27:31: error: no matching function for call to 'eval(int (Bloop::*)(int), Bloop&, int)'
     eval(&Bloop::foo, bloop, 5);
                               ^
main.cpp:27:31: note: candidate is:
main.cpp:19:6: note: template<class R, class C, class ... Args> void eval(R (C::*)(), C&, Args ...)
 void eval (R(C::*func)(), C& c, Args... args) {
      ^
main.cpp:19:6: note:   template argument deduction/substitution failed:
main.cpp:27:31: note:   candidate expects 1 argument, 2 provided
     eval(&Bloop::foo, bloop, 5);
                               ^

如果我像 R(C::*func)(int) 一样声明 func,它会编译。

最佳答案

博客文章中的代码不正确(或至少不完整);它仅适用于无参数函数。您可以像这样更正确地编写 eval:

template<class R, class C, class... T, class... Args>
auto eval(R(C::*f)(T...), C& c, Args&&... args) -> R
{
    return (c.*f)(std::forward<Args>(args)...);
}

注意指向成员函数类型指针的参数的T... 参数包。这是一个不同于 Args&&... 的类型包,因为这两个包的推导方式可能不同。

关于c++ - 使用可变模板参数调用零参数模板函数指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27402591/

相关文章:

c++ - 如何修复这个简单的 Makefile 错误?

c++ - C++ 中的随机段错误

c++ - 使用strcat时访问冲突写入位置错误

C++ 移动类,里面有线程

c++ - 为什么 COW std::string 优化在 GCC 5.1 中仍然启用?

c++11 - 从可变参数类模板为每种类型生成一个方法

c++ - 可变参数模板模板和完美转发

c++ - 初始化列表中积分提升的规则是什么?这是 GCC 还是 C++ 限制中的误报警告?

c++ - std::regex_error - 意外的转义字符

c++ - 模板模板参数的模板参数推导错误