在调试模式下的MSVC 15.8.5上,我明白了
运行时检查失败#2-围绕变量'insert_into'进行堆栈 已损坏。
MSVC中是否存在错误或我做错了什么?
在clang版本6.0.0-1ubuntu2和clang版本7.0.0-svn341916-1〜exp1〜20180911115939.26上运行良好
#include <set>
template <typename... T>
struct Overload : T...
{
//support struct for combining and overloading multiple lambdas
using T::operator()...;
};
template <typename... T>
Overload(T...)->Overload<T...>;
using BuiltSet = std::set<std::string>;
template <typename... Args>
BuiltSet MakeBuildSet(const Args&... args)
{
//takes a list of arguments if an argument is a
// BuiltSet each element is added to headers,
// otherwise the arg itself is added to headers
BuiltSet headers{};
//Construct a lambda with overloading on if argument is a BuiltSet or not
const Overload insert_into{
[](const BuiltSet& h, BuiltSet& headers) {
headers.insert(h.begin(), h.end());
},
[](const auto& arg, BuiltSet& headers) {
headers.insert(arg);
}
};
//Fold over arguments applying the lambda on each element
(insert_into(args, headers), ...);
return headers;
}
int main(int argc, char** argv)
{
MakeBuildSet(MakeBuildSet("a", "b", "c"), "a", "b", "c");
return 0;
}