Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ Binary Literal

I know that Binary Literal in C++ is standardized from C++14.

However, although I fix the std as c++11, it works well. Because actually, I expected an error. The following is my code which I expected an error.

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

Also, I have compiled and executed the above file with the following command.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

g++ -std=c++11 -Wall main.cpp -o runfile
./runfile

What is the reason that an expected result does not come out? Is there something that I’ve got wrong?

>Solution :

Binary literals have been a compiler extension in GCC long before C++14 standardized them. You can compile with -pedantic to warn on extensions and -pedantic-errors to elevate those specific warnings to errors:

<source>:3:11: error: binary constants are a C++14 feature or GCC extension
    3 |   int a = 0b1010; // an error is expected
      |           ^~~~~~
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading