为什么lambda在clang上崩溃而不是在gcc上崩溃
why lambda crash on clang but not gcc?
此程序与clang 一起崩溃
#include <iostream>
#include <string>
#include <mutex>
#include <functional>
int main(int argc, char **argv)
{
auto m = std::function<void()>( [](){
int r = std::rand() % 100;
if (r < 50)
{
return r; //should not return
}
});
for(int i=0;i<100;i++)
m();
return 0;
}
报告:
pt@test$ clang++ -o test test1.cpp -std=c++11
test1.cpp:14:5: warning: control may reach end of non-void lambda [-Wreturn-type]
});
^
1 warning generated.
pt@test$ ./test
Illegal instruction (core dumped)
但是,它不会与g++
崩溃。
我的困惑是,既然它会导致崩溃,为什么clang
不在编译时将其视为错误呢?
更新
考虑以下代码
auto lMayNotReturn = []() {
int rValue = std::rand() % 100;
if (rValue < 50)
{
return rValue; //should not return
}
std::cout << " non-return rValue: " << rValue << std::endl;
};
for(int i=0 ; i< 30;i++){
int rValue= lMayNotReturn();
std::cout << "random value " << rValue << " ";
if (rValue >= 50) {
std::cout << " ???undefined???";
}
std::cout << std::endl;
}
std::cout << "end--------" << std::endl;
由CCD_ 3&visual studio
将继续运行并返回随机值作为
pt@DESKTOP-S54JIQA:/mnt/e/Projects/uv_test$ ./test
non-return rValue: 83
random value 6295680 ???undefined???
non-return rValue: 86
random value 6295680 ???undefined???
non-return rValue: 77
random value 6295680 ???undefined???
像这样的Bug要比简单地崩溃和叮当声获胜更难追踪。
clang
没有引发我能想到的错误的唯一原因是兼容性。
因为这是未定义的行为。对于退货声明:
在没有返回语句的情况下从值返回函数(main除外(的末尾流出是未定义的行为。
这意味着编译器可以做任何事情;他们不需要给出错误(或不给出错误(。
相关文章:
- 当回溯以零开始时,如何调试崩溃
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 内联映射初始化的动态atexit析构函数崩溃
- 执行函数时导致崩溃的变量
- 奇怪的结构&GCC&clang(void*返回类型)
- GCC本机矩阵运算库
- PowerPC ppc64le上的Gcc Woverloaded虚拟错误
- gcc和c++17的过载解析失败
- 程序崩溃并显示"std::out_of_range"错误
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- 如何避免由于内存不足而导致 gcc 崩溃
- 返回向量<对<字符串,字符串>>会导致 gcc 编译代码 (curlcpp) 崩溃
- ofstream在极其简单的程序(GCC/Code::Blocks)中首次输出时崩溃
- GCC 在调试模式下崩溃,在发布模式下运行良好
- 为什么这段代码不能用 clang 构建,用 gcc 崩溃,但在 VC++ 上运行良好?
- 在Windows上使用gcc崩溃后转储堆栈
- clang与gcc运行时的区别:用clang构建的c++类模板在没有复制构造函数的情况下崩溃,用复制构造函数构建的gcc
- Variadic函数在VC2010中失败,并在gcc中崩溃
- 使用GCC 4.5编译的程序崩溃,而GCC 4.4则可以
- 当gcc中的应用程序在没有gdb的情况下崩溃时,如何生成stackdump和转储寄存器值