c++ - 从可变参数函数将函数应用于参数包的所有元素
问题描述
考虑以下(不工作!)示例:
#include <iostream>
template <typename type> void print(const type & item)
{
std :: cout << item << std :: endl;
}
template <typename... types> void printall(const types & ... items)
{
print(items)...;
}
int main()
{
printall(1, 2, "hello");
}
在这里,我有一个print
简单地打印出其参数的函数,以及一个printall
接受一组参数的可变参数函数。现在,我想做的只是简单地printall
应用于print
pack 的每个元素items
。我怎样才能做到这一点?
注意:我不是在问如何打印一组值。我知道折叠表达式的存在,并且我知道我可以将整体items
投入std::cout
使用它们。这里print
只是一个例子,可以是任何函数。
我怎样才能做到这一点?这听起来应该非常简单,但我找不到任何(合理的)语法来做到这一点。
解决方案
我想做的只是简单地
printall
适用
选项1
正如用户@liliscent和用户@max66在评论中建议的那样,在 C++11/C++14 中,您可以使用以下 hacky-way,其行为类似于C++17 中的折叠表达式。
#include <iostream>
template <typename type> void print(const type& item)
{
std::cout << item << '\n';
}
template <typename... types>
void printall (const types&... items)
{
using dummy = int[];
(void)dummy { 0, (print(items), 0)... };
}
选项 - 2
如果上面看起来不够好,提供一个经典的可变参数模板重载作为你的和函数之间的包装器/帮助器,这样每个模板函数参数都可以在.printall()
print()
print()
#include <iostream>
template <typename Type> void print(const Type& item)
{
std::cout << item << '\n'; // print each argument
}
namespace helper
{
void printall() {} // nontemplate overload for last call(i.e, no arguments call)
template<typename FirstArg, typename... Types>
void printall(const FirstArg& firstItem, Types&&... items)
{
::print(firstItem); // call print() for each argument
helper::printall(std::forward<Types>(items)...);// calls the same wrapper::printalll()
}
}
template <typename... Types> void printall(const Types& ... items)
{
helper::printall(items...); // calls the wrapper::printall()
}
选项 - 3
但是,如果您可以访问 C++17,只需使用折叠表达式。这提供了一个干净(非hacky)和更少的代码。
template <typename type> void print(const type& item)
{
std::cout << item << '\n';
}
template <typename... types> void printall(const types&... items)
{
(print(items),...);
}
推荐阅读
- javascript - 如何使用 Vue 类组件访问 VueJS 3 和 Typescript 中的 HTML 引用?
- java - 无法启动服务 jboss.deployment.unit。“LeqadoviProjekt-ear-1.1.ear”。结构:
- python - 我们如何让程序输出一个int?
- node.js - gulp-git 从状态读取响应
- mysql - MySQL 上的 INTERVAL '0-0' YEAR_MONTH 是什么意思?
- actions-on-google - 在提示输出中包括会话存储日期和/或时间
- php - 开发我的 Web 应用程序时出现 EasyAdmin Bundle 问题
- excel - 没有为具有数值的字符串参数调用 UDF
- javascript - 如果文本进入下一行,则截断文本(调整窗口大小)
- node.js - 如何在nodejs中保存文件?