我有一个模板函数,可以将其放入输出流,而不必担心类型。这是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();
}
将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();
}