下面的代码段进行编译,
#include<sys/types.h>
#include<sys/wait.h>
#include<iostream>
int main()
{
int ret = 0xFFFF;
std::cout << WEXITSTATUS(ret);
}
然而,此代码段并没有使用G++ 4.9.4
进行编译:
#include<sys/types.h>
#include<sys/wait.h>
#include<iostream>
int main()
{
std::cout << WEXITSTATUS(0xFFFF);
}
编译器抱怨如下:
In file included from /usr/include/x86_64-linux-gnu/sys/wait.h:77:0,
from t.cpp:2:
t.cpp: In function ‘int main()’:
t.cpp:7:22: error: lvalue required as unary ‘&’ operand
std::cout << WEXITSTATUS(0xFFFF);
^
以下是有关编译器的详细信息:
g++ --version
g++ (Ubuntu 4.9.4-2ubuntu1~16.04) 4.9.4
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
编译器通过以下命令安装在Ubuntu16.04上
sudo apt-get install gcc-4.9
sudo apt-get install g++-4.9
sudo update-alterntives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 20
sudo update-alterntives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 20
注:
奇怪的是,我无法在godbolt.org
上重现上述现象.It compiles on godbolt.org和gcc 4.9.3
(gcc 4.9.4
不可用).
这是g++ -E the_said_code_snippet_does_not_compile.cpp
的输出
//omit
# 4 "t.cpp" 2
int main()
{
std::cout << ((((*(const int *) &(0xFFFF))) & 0xff00) >> 8);
}
有人能解释一下这件事吗?
100:
我现在可以重现错误了!请参见此link.
100:
这只是一个简单的例子.我实际上面对的是WEXITSTATUS(pclose(fp))
不编译.