Book Image

Boost.Asio C++ Network Programming

By : Wisnu Anggoro
Book Image

Boost.Asio C++ Network Programming

By: Wisnu Anggoro

Overview of this book

Table of Contents (15 chapters)
Boost.Asio C++ Network Programming Second Edition
Credits
About the Authors
Acknowledgements
About the Reviewers
www.PacktPub.com
Preface
Index

Knowing other important options in the GCC C++ compiler


GCC supports ISO C++ 1998, C++ 2003, and also C++ 2011 standard in version 4.9.2. Selecting this standard in GCC is done using one of these options: -ansi, -std=c++98, -std=c++03, or –std=c++11. Let's look at the following code and give it the name hash.cpp:

/* hash.cpp */
#include <iostream>
#include <functional>
#include <string>
int main(void) {
  std::string plainText = "";
  std::cout << "Input string and hit Enter if ready: ";
  std::cin >> plainText;
  std::hash<std::string> hashFunc;
  size_t hashText = hashFunc(plainText);
  std::cout << "Hashing: " << hashText << "\n";
  return 0;
}

If you compile and run the program, it will give you a hash number for every plain text user input. However, it is little tricky to compile the preceding code. We have to define which ISO standard we want to use. Let's take a look at the following five compilation commands and try them one by one in our Command Prompt window:

g++ -Wall hash.cpp -o hash
g++ -Wall -ansi hash.cpp -o hash
g++ -Wall -std=c++98 hash.cpp -o hash
g++ -Wall -std=c++03 hash.cpp -o hash
g++ -Wall -std=c++11 hash.cpp -o hash

When we run the first four preceding compilation commands, we should get the following error message:

hash.cpp: In function 'int main()':
hash.cpp:10:2: error: 'hash' is not a member of 'std'
  std::hash<std::string> hashFunc;
hash.cpp:10:23: error: expected primary-expression before '>' token
  std::hash<std::string> hashFunc;
hash.cpp:10:25: error: 'hashFunc' was not declared in this scope
  std::hash<std::string> hashFunc;

It says that there is no hash in the std class. Actually, this is not true as a hash has been defined in the header <string> since C++ 2011. To solve this problem, we can run the last preceding compilation command, and if it does not throw an error anymore, then we can run the program by typing hash in the console window.

As you can see in the preceding screenshot, I invoked the program twice and gave Packt and packt as the input. Although I just changed a character, the entire hash changed dramatically. This is why hashing is used to detect any change in data or a file if they are transferred, just to make sure the data is not altered.

For more information about ISO C++11 features available in GCC, go to http://gcc.gnu.org/projects/cxx0x.html. To obtain all the diagnostics required by the standard, you should also specify the -pedantic option (or the -pedantic-errors option if you want to handle warnings as errors).

Note

The -ansi option alone does not cause non-ISO programs to be rejected gratuitously. For that, the -pedantic option or the -pedantic-errors option is required in addition with the -ansi option.