MSVC抛出“运行时检查失败#2”(损坏的堆栈),用于重载/ std::visit模式(C++17)

cig3rfwq  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(92)

我使用“overload”模式为std::variant创建一个访问者,将方法定义为带有泛型lambda的类间das,以提供一个空的默认方法。

// imagine declaration of the overload template + the deduction helper here ...

using Foodstuff = std::variant<Fruit, Vegetable, Meat, Dairy, Fish>;

int things_eaten{0};

auto picky_eater = overload{
    // Eats fruit
    [&things_eaten](const Fruit&) { things_eaten++; },
    // Eats dairy
    [&things_eaten](const Dairy&) {things_eaten++; },

    // Doesn't eat anything else - I need an empty handler for the other foodstuffs
    [](auto) {}
};

字符串
然后我用它来std::visit一个食物的向量。在MSVC 2022(v17.7.6)中编译这个,目标是C17语言标准,我没有得到编译器警告(at /W3),程序按照我在“release”目标中的预期运行,但是当它退出函数时,我将其更改为debug,我得到了异常
运行时检查失败#2 -变量'picky_eater'周围的堆栈已损坏。
注意到其他的lambda有一个捕获,而最后的泛型lambda没有,如果我把最后一个重载改为[&things_eaten](auto) {},这个异常就消失了。
此外,如果我将语言标准更改为C
20,原始版本不会生成该异常。
这个异常是否在原始代码中识别出了一个真实的问题,C20是否引入了一些东西(也许是模板演绎的变化?)来解决它?这是MSVC C17在调试buuilds中运行时检查的一个奇怪的bug吗(我没有其他编译器或分析工具可以比较)。

#include <variant> 
#include <vector>
#include <string>
#include <iostream>

// Overload pattern

template <typename... Ts>
struct overload : Ts...
{
    using Ts::operator()...;
};

// Template deduction helper; needed in C++17
template <typename... Ts>
overload(Ts...)->overload<Ts...>;

///////////////////////////////////////////////////////////

struct Fruit {
    std::string name;
};

struct Vegetable {
    std::string name;
};

struct Meat {
    std::string name;
};

struct Dairy {
    std::string name;
};

struct Fish {
    std::string name;
};

using Foodstuff = std::variant<Fruit, Vegetable, Meat, Dairy, Fish>;
using Meal = std::vector<Foodstuff>;

int main() {
    auto meal = Meal{ Fruit{"Apple"}, Vegetable{"Tomato"}, Meat{"Beef"}, Vegetable{"Potato"}, Dairy{"Yoghurt"} };

    int things_eaten{ 0 };

    auto picky_eater = overload{
        // Eats fruit
        [&things_eaten](const Fruit&) { things_eaten++; },

        // Eats dairy
        [&things_eaten](const Dairy&) { things_eaten++; },

        // Doesn't eat anything else - I need an empty handler for the other foodstuffs

        // If I write the generic lambda this way, MSVC in C++17 langauge standard will
        // give an exception in Debug builds:
        // "Run-Time Check Failure #2 - Stack around the variable 'picky_eater' was corrupted."
        [](auto) {}

        // But if I just add the same capture, even though it's not used, that error goes away?
        // [&things_eaten](auto) {}
    };

    for (const auto& ingredient : meal) {
        std::visit(picky_eater, ingredient);
    }

    std::cout << "I ate " << things_eaten << " things" << std::endl;
    return 0;
    // As the program exits, we get the exception
}

kqlmhetl

kqlmhetl1#

您的代码看起来是正确的,可以进一步简化为

template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded( Ts... )->overloaded<Ts...>;

int main() {
  int i{ 0 };
  auto lambda = overloaded{
    [&i](int) {},
    [](auto) {}
  };
}

字符串
在Visual Studio中保留堆栈损坏错误。
我向MS团队报告:https://developercommunity.visualstudio.com/t/Run-Time-Check-Failure-2---Stack-around/10510860
根据他们的repospone,这是一个真实的编译器错误,最早在2019年报告:https://developercommunity.visualstudio.com/t/Capturing-lambdas-in-function-object-res/475396

相关问题