将适当数量和类型的参数传递给函数

时间:2016-04-26 09:51:00

标签: c++ templates c++11 metaprogramming

我想知道在C ++ 11中是否甚至可以做这样的事情,将适当数量和类型的参数传递给函数,例如你有:

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

void foo(std::string first, double second, std::string third);
void bar(std::string first, std::string second);
void baz(std::string first, int c);

void passArgs(std::vector<std::string> arguments)
{
    //Get arguments count for function foo, do the same for the others
    size_t foo_count = decltype(getArgumentCount(foo))::value;
    //Here pass appropriate amount of arguments to foo,bar and baz and 
    //convert to appropriate type using for example std::stoi and 
    //std::stod when the argument is int or double
    Magic(foo,arguments,foo_count);
}

int main()
{


}

提前谢谢。

1 个答案:

答案 0 :(得分:4)

#include <type_traits>
#include <utility>
#include <string>
#include <cstddef>
#include <vector>

template <typename T>
T convert(const std::string&);

template <>
int convert<int>(const std::string& s)
{
    return std::stoi(s);
}

template <>
std::string convert<std::string>(const std::string& s)
{
    return s;
}

template <>
double convert<double>(const std::string& s)
{
    return std::stod(s);
}

template <typename R, typename... Args, std::size_t... Is>
void Magic(const std::vector<std::string>& arguments, R(*f)(Args...), std::index_sequence<Is...>)
{
    f(convert<typename std::decay<Args>::type>(arguments[Is])...);
}

template <typename R, typename... Args>
void passArgs(const std::vector<std::string>& arguments, R(*f)(Args...))
{
    Magic(arguments, f, std::make_index_sequence<sizeof...(Args)>{});
}

测试:

int main()
{
    std::vector<std::string> arguments{"abc", "3.14", "def"};
    passArgs(arguments, &foo);
    passArgs(arguments, &bar);
    passArgs(arguments, &baz);
}

DEMO

对于符合C ++ 11的实现,您可以使用index_sequence

的强制实现
template <std::size_t...> 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...> {};

DEMO 2