在可变函数调用中执行一次语句
Execute a statement once in a variadic function call
为了输出有用的信息,我有以下Log
class:
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;
}
}
}
为了输出有用的信息,我有以下Log
class:
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;
}
}
}