【问题标题】:Pass appropriate amount and type of arguments to a function将适当数量和类型的参数传递给函数
【发布时间】:2016-08-20 01:40:27
【问题描述】:

我想知道这样的事情在 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()
{


}

提前谢谢你。

【问题讨论】:

  • 您正在尝试做的事情非常复杂且有问题。如果可能的话,使用继承会更容易;具有可以使用的方法 numArgs() 和 argType( unsigned int index ) 的基类,然后在汇编中执行一些函数技巧(将 args 推入堆栈),或者创建一个模板派生类,然后向下转换为调用函数。
  • 你怎么知道字符串"3.54"是文本"3.54"还是double?只是假设任何可以是 int 的东西,如果是双精度数,则不是字符串?您可以使用 boost::lexical_cast&lt;&gt;(或您自己的实现 - 使用 istringstream 占用约 5 行代码)进行转换。不过,您将需要在函数调用之间使用某种switch,或者可能是参数数量及其类型的数字编码,您可以将其传递给知道可用函数并让它选择匹配的模板参数列表。
  • @Tont D 你知道函数签名必须是什么类型,例如foofirst必须是stringseconddoublethirdstringstd::vector&lt;std::string&gt; arguments 中的参数顺序正确。

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


【解决方案1】:
#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

【讨论】:

  • 可能 OP 想要在将参数提供给函数之前从前面弹出参数;这很容易通过让passArgs 获取两个template&lt;class RandomAccessIterator&gt;s,并让它返回最后一次使用的RandomAccessIterator
  • 感谢这个解决方案,但是在 C++11 中,而不是 C++14 中是否可行?
  • @Mayhem 是的,只需将 index_sequence 替换为您喜欢的任何自定义实现,demo
  • 非常感谢!您能否在答案中添加 C++11 替换 index_sequence 以确保完整性?
猜你喜欢
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
  • 2017-04-15
  • 1970-01-01
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
相关资源
最近更新 更多