Variadic template resolution in VS2013 - Error C3520

孤街浪徒 提交于 2021-02-17 00:07:24

问题


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:

  1. rest needs to be expanded:

    Write(level, ss, rest...);
    
  2. Write is supposed to take a log level as its first argument:

    Write(GetLogLevel(), std::stringstream(), "Hello", (const char*)" World!", 1);
    
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!