Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Boost.Asio C++ Network Programming

You're reading from   Boost.Asio C++ Network Programming Learn effective C++ network programming with Boost.Asio and become a proficient C++ network programmer

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher
ISBN-13 9781785283079
Length 200 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Wisnu Anggoro Wisnu Anggoro
Author Profile Icon Wisnu Anggoro
Wisnu Anggoro
Arrow right icon
View More author details
Toc

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.

Knowing other important options in the GCC C++ compiler

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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image