[英]pass multiple arguments to another function using template variadic function
让我们考虑以下功能:
static void Print(const Type& type, const std::string& message, const std::string& variable) {
Log(type, message + ": " + variable);
}
我希望它传递任意数量的变量(我的意思是std::string &
variable - 它保存一个变量名),然后通过Log()
函数一起发送它们,因此我考虑使用模板可变参数函数(重载的Print()
)。 我这样定义它:
template <typename Arg, typename ...Args)
static void Print(const Type& type, const std::string& message,
const Arg& arg, const Args&... args);
接着:
Print(type, message, args...);
Log(type, message + ": " + arg);
只是一个想法,这很可能会像这样:
args...
将被传递, Print()
函数将被递归调用,直到没有参数为止, Log()
函数,它基本上每次都会记录它。 我需要做的是以某种方式记住arg
值,但它需要使用另一个参数调用Print()
,我真的不喜欢这个想法。 你还有其他线索吗?
根据所需的格式,您可以使用折叠表达式:
template<class... Args>
void Print(const Type& type, const std::string& message, const Args&... arg)
{
std::stringstream strstr;
strstr << message << ": "; // Or your prefix computation, whatever you want.
((strstr << arg << ", "), ...);
std::string toLog = strstr.str();
// Remove last separator characters.
toLog.erase(toLog.end() - 2, toLog.end());
Log(type, strstr.str());
}
在我看来,Max Langhof的解决方案简单而优雅。
不幸的是,它使用的模板折叠仅从C ++ 17开始提供。
我提出了一个C ++ 11 / C ++ 14版本,而不是模板折叠,它使用了未使用数组初始化的旧技巧
template <typename ... Args>
void Print (Type const & type, std::string const & message,
Args const & ... arg)
{
using unused = int[];
std::stringstream strstr;
strstr << message << ": ";
(void)unused { 0, (strstr << arg << ", ", 0)... };
std::string toLog = strstr.str();
// Remove last separator characters.
toLog.erase(toLog.end() - 2, toLog.end());
Log(type, strstr.str());
}
我简化了你的例子,所以假设我正确地理解你想做什么,你可以做以下两个解决方案之一,如果你的编译器不支持@Max Langhof建议的C ++ 17折叠。
它们都可以在任何支持operator +的类型上运行,但是如果你的concat函数是其他东西,它们很容易修改。
template <typename Arg>
static void Print(const Arg& message, const Arg& arg1)
{
Log(message + ": " + arg1);
}
template <typename Arg, typename... Args>
static void Print(const Arg& message, const Arg& arg1, const Arg& arg2, const Args&... variables)
{
Print(message, arg1 + ", " + arg2, variables...);
}
template <typename Arg, typename... Args>
static void Print2(const Arg& message, const Arg& arg1, const Args&... variables)
{
std::vector<Arg> args = { variables... };
Arg result = std::accumulate(args.begin(), args.end(), arg1, [](const Arg& a, const Arg& b) {
return a + ", " + b;});
Log(message + ": " + result);
}
请注意,此版本将在std :: vector中创建参数的副本,而不像其他解决方案那样。
这两个示例都可以按以下方式使用:
static void Log(const std::string& m)
{
std::cout << m << std::endl;
}
int main()
{
std::string msg = "MyMessage1";
std::string var1 = "Var1";
std::string var2 = "Var2";
std::string var3 = "Var3";
std::string var4 = "Var4";
std::string var5 = "Var5";
Print(msg, var1);
Print(msg, var1, var2);
Print(msg, var1, var2, var3);
Print(msg, var1, var2, var3, var4);
Print(msg, var1, var2, var3, var4, var5);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.