c++ - 在可变参数函数调用中执行一次语句
问题描述
Log
为了输出有用的信息,我有以下课程:
class Log
{
public:
enum {ALL = 0, DEBUG, ERROR};
static void setDebugMode()
{
mDebugMode = true;
}
template <class T, class ... Args>
static void write(unsigned int level, T&& arg, Args&& ... args)
{
switch (level)
{
//Important piece of information that needs to be displayed
case ALL:
{
std::cout << std::forward<T>(arg);
write(level, out, std::forward<Args>(args) ...);
break;
}
//For reporting errors
case ERROR:
{
std::cerr << std::forward<T>(arg);
write(level, out, std::forward<Args>(args) ...);
break;
}
//Additional information that is available in debug mode
case DEBUG:
{
if(!mDebugMode)
break; //If debug mode not set, get out
std::cout << std::forward<T>(arg);
write(level, out, std::forward<Args>(args) ...);
break;
}
}
}
template <class T>
static void write(unsigned int level, T&& t)
{
switch(level)
{
case ALL:
{
std::cout << std::forward<T>(t) << std::endl;
break;
}
case DEBUG:
{
std::cout << std::forward<T>(t) << std::endl;
break;
}
case ERROR:
{
std::cerr << std::forward<T>(t) << std::endl;
break;
}
}
}
private:
Log();
static bool mDebugMode;
};
我的问题如下:如何在不打印每个函数调用的情况下将“(DEBUG)”一次输出到控制台?也就是说,如果我使用传入的write
函数作为级别,我想获得以下输出:DEBUG
(DEBUG) 13852 files loaded
解决方案
您可以通过使用折叠表达式而不是递归实现来简化代码并简化此前缀。(折叠表达式需要 C++17 编译器/编译器模式。)
// The only write function needed:
template <class ... Args>
static void write(unsigned int level, Args&& ... args)
{
switch (level)
{
//Important piece of information that needs to be displayed
case ALL:
{
(std::cout << ... << std::forward<Args>(args));
break;
}
//For reporting errors
case ERROR:
{
(std::cerr << ... << std::forward<Args>(args));
break;
}
//Additional information that is available in debug mode
case DEBUG:
{
if(!mDebugMode)
break; //If debug mode not set, get out
std::cout << "(DEBUG) ";
(std::cout << ... << std::forward<Args>(args));
break;
}
}
}
推荐阅读
- coronasdk - 关于 Solar2D/CoronaSDK 如何像游戏傻瓜部队一样在同一组中添加多个蒙版
- c - 为什么我的金字塔中间没有空间?
- python-3.x - 使用 GO 网络服务器解析 POST 请求时出现问题
- python - 查找对 (i,j) 的数量,使得 i < j 且 A[i] >= 2A[j]
- reactjs - 网站某部分的整页垂直轮播效果
- java - Android Studio Recyclerview 更新时冻结
- javascript - 在产品页面自定义字段中检索产品自定义字段值
- spring-boot - 在 Spring Data Rest 中,如何防止从我的 JpaRepository 中导出 DELETE HTTP 方法?
- python-3.x - 官方 Airflow Docker 映像无法识别气流命令
- node.js - 如果新上传的图像达到限制(文件变大等),Multer 会删除上传的图像