10

Недавно был вопрос о том, как объявить функцию с n аргументами заданного типа. У меня возник противоположный вопрос. Как узнать количество аргументов функции?

#include <iostream>

void foo(float, int){

}

int main(){
    std::cout << doTemplateMagic(foo); //2
}
3
  • А что выводить для printf, например? :)
    – Harry
    20 дек 2017 в 11:54
  • 1
    @Harry, ошибка компиляции будет в самый раз 20 дек 2017 в 11:55
  • @Harry, хотя, думаю, там можно будет что-нибудь специфицировать или перегрузить для функций, у которых в конце элипсисис идет, и выдавать -1. Но для простоты можно не думать об этом 20 дек 2017 в 11:58

3 ответа 3

6

Можно использовать темплейт с переменным числом параметров:

#include <iostream>

template <typename R, typename ... Types> constexpr size_t getArgumentCount( R(*f)(Types ...))
{
   return sizeof...(Types);
}

//----------------------------------    
// Test it out with a few functions.
//----------------------------------    

void foo(int a, int b, int c)
{
}

int bar()
{
   return 0;
}

int baz(double)
{
   return 0;
}

int main()
{
    std::cout << getArgumentCount(foo) << std::endl;
    std::cout << getArgumentCount(bar) << std::endl;
    std::cout << getArgumentCount(baz) << std::endl;
    return 0;
}

Вывод:

3
0
1

Или использовать этот метод:

template <typename R, typename ... Types> 
constexpr std::integral_constant<unsigned, sizeof ...(Types)> getArgumentCount( R(*f)(Types ...))
{
   return std::integral_constant<unsigned, sizeof ...(Types)>{};
}

// Guaranteed to be evaluated at compile time
size_t count = decltype(getArgumentCount(foo))::value;

или

// Most likely evaluated at compile time
size_t count = getArgumentCount(foo).value;

Источник: https://stackoverflow.com/questions/36797770/get-function-parameters-count

4

Boost.TypeTraits, например

#include <type_traits>
#include <boost/type_traits.hpp>

void f(int, char) {}

int n = boost::function_traits<std::remove_pointer_t<decltype(&f)>>::arity;
1

Немного покритикую остальные ответы, приводя цитату:

“Don't try to write helper code to detect PMFs/PMDs and dispatch on them -- it is an absolute nightmare. PMF types are the worst types by far in the core language.”

Используйте Boost.CallableTraits

#include <type_traits>
#include <boost/callable_traits.hpp>

namespace ct = boost::callable_traits;

int main() {
    auto lamda = [](int, float&, const char*){};
    using lam = decltype(lamda);
    static_assert(std::tuple_size_v<ct::args_t<lam>> == 3);
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.