C++ 二进制文字

JIN*_*JIN 1 c++ binary c++14

我知道C++ 中的Binary Literal是从C++14标准化的。

然而,虽然我修复了stdas c++11,但它运行良好。因为实际上,我预料到会出现错误。以下是我的代码,我预计会出现错误。

int main(){
  int a = 0b1010; // an error is expected
  std::cout << a << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

另外,我已经使用以下命令编译并执行了上述文件。

g++ -std=c++11 -Wall main.cpp -o runfile
./runfile
Run Code Online (Sandbox Code Playgroud)

到底是什么原因导致没有达到预期的结果呢?难道是我哪里做错了?

chr*_*ris 9

早在 C++14 对其进行标准化之前,二进制文字就已经成为 GCC 中的编译器扩展。您可以编译以-pedantic警告扩展并将-pedantic-errors这些特定警告提升为错误:

<source>:3:11: error: binary constants are a C++14 feature or GCC extension
    3 |   int a = 0b1010; // an error is expected
      |           ^~~~~~
Run Code Online (Sandbox Code Playgroud)