为何自定义YCombinator在VS关优化正常,开/Ox优化后行为异常?
类Y组合子Lambda封装实验的Release优化异常问题
我正在开展类似Y组合子的lambda封装实验,据我所知这类实现严格来说并非标准Y组合子,但遇到了非常反常的问题:代码在Debug配置(关闭优化)下运行完全符合预期,但在开启「优化(优先速度)(/Ox)」的Release配置下,会跳过大量关键代码段。
说明:lambda函数内部的逻辑基本无关紧要,仅用于验证递归等功能的正确性。
实验用的完整代码如下:
// main.cpp #include <iostream> #include <string> #define uint unsigned int // Defines a y-combinator-style thing to do recursive things. Includes a system where the lambda can declare itself to be obsolete. // Yes, it's hacky and ugly. Don't worry about it, this is all just testing functionality. template <class F> class YCombinator { public: F m_f; // the lambda will be stored here bool m_selfDestructing = false; //!< Whether the combinator will self-destruct should its lambda mark itself as no longer useful. bool m_selfDestructTrigger = false; //!< Whether the combinator's lambda has marked itself as no longer useful. // a forwarding operator: template <class... Args> decltype(auto) evaluate(Args&&... args) { // Avoid storing return if we can, if (!m_selfDestructing) { // Pass itself to m_f, then the arguments. return m_f(*this, std::forward<Args>(args)...); } else { // Pass itself to m_f, then the arguments. auto r = m_f(*this, std::forward<Args>(args)...); // self-destruct if necessary, allowing lamdas to delete themselves if they know they're no longer useful. if (m_selfDestructTrigger) { delete this; } return r; } } }; template <class F> YCombinator(F, bool sd)->YCombinator<F>; // Tests some instances. int main() { // Most basic test auto a = YCombinator{ [](auto & self, uint in)->uint{ uint out = in; for (uint i = 1u; i < in; ++i) { out += self.evaluate(i); } return out; }, false }; // Same as a, but checks it works as a pointer. auto b = new YCombinator{ [](auto & self, uint in)->uint { uint out = in; for (uint i = 0u; i < in; ++i) { out += self.evaluate(i); } return out; }, false }; // c elided for simplicity // Checks the self-deletion mechanism auto d = new YCombinator{ [&a, b](auto & self, uint in)->uint { std::cout << "Running d(" << in << ") [SD-" << self.m_selfDestructing << "]..." << std::endl; uint outA = a.evaluate(in); uint outB = b->evaluate(in); if (outA == outB) std::cout << "d(" << in << ") [SD-" << self.m_selfDestructing << "] confirmed both a and b produced the same output of " << outA << "." << std::endl; self.m_selfDestructTrigger = true; return outA; }, true }; uint resultA = a.evaluate(4u); std::cout << "Final result: a(4) = " << resultA << "." << std::endl << std::endl; uint resultB = (*b).evaluate(5u); std::cout << "Final result: b(5) = " << resultB << "." << std::endl << std::endl; uint resultD = d->evaluate(2u); std::cout << "Final result: d(2) = " << resultD << "." << std::endl << std::endl; resultD = d->evaluate(2u); std::cout << "Final result: d(2) = " << resultD << "." << std::endl << std::endl; }
预期行为
第一次调用d的evaluate方法时可正常运行,将d.m_selfDestructTrigger设为true并触发自身删除;第二次调用d的evaluate方法时应当触发未定义行为(更准确来说并非必然崩溃,Debug配置下的实际表现为直接崩溃),因为d指向的对象已被销毁。
实际异常表现
在Release配置下,观察到evaluate方法中的所有逻辑似乎被完全跳过,执行流直接跳转到lambda调用。优化后代码的断点参考性有限,但从运行输出的现象来看确实符合上述特征。尝试重新生成项目后问题依旧,Visual Studio的表现十分反常。
当前存疑的问题
- 是否存在认知偏差?
- 是否遗漏了代码层面的逻辑问题?
- 该现象是代码本身的逻辑缺陷导致,还是Visual Studio(乃至编译器)的实际bug?
希望能得到协助判断问题根源。
环境信息:使用Visual Studio 2019 16.8.3版本,启用了
/std:c++ latest特性集
内容的提问来源于stack exchange,提问作者Stefan Bauer
相关产品推荐
相关产品推荐

