问题
On MSVC 15.8.5 in debug mode I get that
Run-Time Check Failure #2 - Stack around the variable 'insert_into' was corrupted.
Is there a bug in MSVC or have I done something bad?
Runs fine on clang version 6.0.0-1ubuntu2 and clang version 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;
}
回答1:
MSVC bug so it is a flaw in the compiler.
来源:https://stackoverflow.com/questions/52833810/stack-around-the-variable-was-corrupted