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

Pass appropriate amount and type of arguments to a function

本文关键字:参数传递 函数 类型      更新时间:2023-10-16

我想知道在 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()
{

}

提前谢谢你。

#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);
}

演示


对于符合 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...> {};

演示 2