考虑这个代码示例:
#include <initializer_list>
#include <iostream>
int main()
{
for(auto e: []()->std::initializer_list<int>{return{1,2,3};}())
std::cout<<e<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我尝试用g ++编译它(gcc版本4.9.2(Debian 4.9.2-10)),输出正确.在clang ++中(Debian clang版本3.5.0-9(标签/ RELEASE_350/final)(基于LLVM 3.5.0))输出例如:
0
2125673120
32546
Run Code Online (Sandbox Code Playgroud)
第一行总是0,最后两行是"随机".
这是clang或其他什么的错误?我认为这个代码示例是正确的.
更新:
当lambda函数返回类型是别的东西(例如std :: vector或std :: array)时,这段代码工作正常.
考虑这个代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
//this file exists and contains data: "ABCDEFGHIJKLM"
FILE* file = fopen("file.txt", "r");
char data[4];
long int pos = ftell(file);
fseek(file, 0, SEEK_SET);
fread(data, 4, 1, file);
fseek(file, pos, SEEK_SET);
printf("ftell: %d\n", ftell(file));
printf("lseek: %d\n", lseek(fileno(file), 0, SEEK_CUR));
fread(data, 1, 4, file);
//this correctly prints A
//but external function needs fileno(file) that has wrong pos
printf("%c\n", data[0]);
fclose(file);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该计划的结果令人惊讶:
ftell: 0
lseek: 14
A
Run Code Online (Sandbox Code Playgroud)
我正在尝试修复我的应用程序中的错误,但我确定前一段时间此程序的结果应该是0, 0(换句话说,从来没有在我的应用程序中出现此错误). …