问题
What's wrong with this code?
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);
I tried to create a recursive/variadic template for logging just as in MSDN, but I just can't rid of error C3520 (C3520: 'P' : parameter pack must be expanded in this context.
). Is there something I'm doing wrong, or is it (god forbid) a compiler bug?
回答1:
I see the following issues:
rest
needs to be expanded:Write(level, ss, rest...);
Write
is supposed to take a log level as its first argument:Write(GetLogLevel(), std::stringstream(), "Hello", (const char*)" World!", 1);
You can't pass a temporary
std::stringstream
to an lvalue reference:std::stringstream ss; Write(GetLogLevel(), ss, "Hello", (const char*)" World!", 1);
回答2:
You probably need
Write(level, ss, rest...);
// ^^^ Note these dots!
来源:https://stackoverflow.com/questions/24683732/variadic-template-resolution-in-vs2013-error-c3520