How to call a function object differently, depending on its arity (or other information known at compile time)?

瘦欲@ 提交于 2019-12-04 19:26:08

With C++11:

#include <iostream>

template <typename F> struct Traits;

template <typename R, typename... A>
struct Traits<R (A...)>
{
    static constexpr unsigned Arity = sizeof...(A);
};

void f(int, int, int);

int main() {
    std::cout
        << Traits<void()>::Arity
        << Traits<void(int)>::Arity
        << Traits<void(int, int)>::Arity
        << Traits<decltype(f)>::Arity
        << '\n';
    return 0;
}

Otherwise you might lookup boost::function: http://www.boost.org/doc/libs/1_55_0b1/doc/html/function.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!