这段代码出了什么问题?
enum LogLevel {
LogLevel_Error = 1,
LogLevel_Warning = 2,
LogLevel_Info = 3,
LogLevel_Debug = 4
};
LogLevel GetLogLevel() {return LogLevel_Debug;};
void Write(const std::string& message) {};
void Write(LogLevel level, std::stringstream& ss) {
if (level > GetLogLevel())
return;
Write(ss.str());
}
template<typename Arg> void Write(LogLevel level, std::stringstream& ss, Arg arg) {
if (level > GetLogLevel())
return;
ss << arg;
Write(ss.str());
}
template<typename First, typename... Rest> void Write(LogLevel level, std::stringstream& ss, First first, Rest... rest) {
if (level > GetLogLevel())
return;
ss << first;
Write(level, ss, rest); // Error C3520, see below
}
Write(std::stringstream(), "Hello", (const char*)" World!", 1);
我尝试创建一个用于记录just as in MSDN的递归/可变参数模板,但我无法摆脱错误C3520(C3520: 'P' : parameter pack must be expanded in this context.
)。有什么我做错了,还是(上帝禁止)编译错误?
答案 0 :(得分:4)
我看到以下问题:
rest
需要扩展:
Write(level, ss, rest...);
Write
应该将日志级别作为其第一个参数:
Write(GetLogLevel(), std::stringstream(), "Hello", (const char*)" World!", 1);
您无法将临时std::stringstream
传递给左值参考:
std::stringstream ss;
Write(GetLogLevel(), ss, "Hello", (const char*)" World!", 1);
答案 1 :(得分:2)
您可能需要
Write(level, ss, rest...);
// ^^^ Note these dots!