错误:仅在-std = c ++ 1y或-std = gnu ++ 1y [-Werror]

问题描述 投票:0回答:1
中使用lambda参数声明中的'auto'

我有一个模板函数,可以将其放入输出流,而不必担心类型。这是C ++ 14兼容代码,具有自动作为lambda的参数。但是,我需要将编译器设置设置为C ++11。我需要进行哪些更改来解决此问题,以便它也可以与C ++ 11一起使用。

这是我的代码

template<class... Args >
std::string build_message( Args&&... args )
{

    auto aPrintImplFn = [](auto& os, auto&& ... ts) {
        // expression (void) just to suppress the unused variable warning
        (void)std::initializer_list<char> { (os << ts, '0')... };
    };

    std::ostringstream out;
    aPrintImplFn(out, std::forward<Args>(args)...);
    return out.str();
}
c++ c++11 variadic-templates auto
1个回答
0
投票

auto更改为显式类型,并使用函子,函数指针或如下所示

#include <sstream>
#include <string>
#include <iostream>

template<class... Args >
void aPrintImplFn (std::ostringstream& os, Args&& ... ts){
    // expression (void) just to suppress the unused variable warning
    (void)std::initializer_list<char> { (os << ts, '0')... };
}
template<class... Args >
std::string build_message( Args&&... args )
{

    std::ostringstream out;
    aPrintImplFn(out, std::forward<Args>(args)...);
    return out.str();
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.