Reputation: 507
I'm wondering if such a thing is even possible in C++11, passing appropriate amount and type of arguments to a function when for example you have:
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()
{
}
Thank you in advance.
Upvotes: 2
Views: 113
Reputation: 48487
#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)>{});
}
Test:
int main()
{
std::vector<std::string> arguments{"abc", "3.14", "def"};
passArgs(arguments, &foo);
passArgs(arguments, &bar);
passArgs(arguments, &baz);
}
For a C++11-compliant implementation, you could employ the blow implementation of 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...> {};
Upvotes: 4