Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying C++ System Programming Cookbook
  • Table Of Contents Toc
C++ System Programming Cookbook

C++ System Programming Cookbook

By : Onorato Vaticone
3.8 (4)
close
close
C++ System Programming Cookbook

C++ System Programming Cookbook

3.8 (4)
By: Onorato Vaticone

Overview of this book

C++ is the preferred language for system programming due to its efficient low-level computation, data abstraction, and object-oriented features. System programming is about designing and writing computer programs that interact closely with the underlying operating system and allow computer hardware to interface with the programmer and the user. The C++ System Programming Cookbook will serve as a reference for developers who want to have ready-to-use solutions for the essential aspects of system programming using the latest C++ standards wherever possible. This C++ book starts out by giving you an overview of system programming and refreshing your C++ knowledge. Moving ahead, you will learn how to deal with threads and processes, before going on to discover recipes for how to manage memory. The concluding chapters will then help you understand how processes communicate and how to interact with the console (console I/O). Finally, you will learn how to deal with time interfaces, signals, and CPU scheduling. By the end of the book, you will become adept at developing robust systems applications using C++.
Table of Contents (13 chapters)
close
close

Automatic type deduction and decltype

C++ offers two mechanisms for deducting types from an expression: auto and decltype(). auto is used to deduce a type from its initializer, while decltype() is used to deduce a type for more complex cases. This recipe will show examples of how to use both.

How to do it...

It might be handy (and it actually is) to avoid explicitly specifying the type of variable that will be used, especially when it is particularly long and used very locally:

  1. Let's start with a typical example:
std::map<int, std::string> payslips;
// ...
for (std::map<int,
std::string>::const_iterator iter = payslips.begin();
iter !=payslips.end(); ++iter)
{
// ...
}
  1. Now, let's rewrite it with auto:
std::map<int, std::string> payslips;
// ...
for (auto iter = payslips.begin(); iter !=payslips.end(); ++iter)
{
// ...
}
  1. Let's look at another example:
auto speed = 123;         // speed is an int
auto height = calculate (); // height will be of the
// type returned by calculate()

decltype() is another mechanism offered by C++ that can deduce the type of expression when the expression is more complex than the auto case.

  1. Let's look at this using an example:
decltype(a) y = x + 1;  // deducing the type of a
decltype(str->x) y; // deducing the type of str->x, where str is
// a struct and x
// an int element of that struct

Could we use auto instead of decltype() in these two examples? We'll take a look in the next section.

How it works...

The first example with auto shows that the type is deduced, at compile time, from the right-hand parameter. auto is used in simple cases.

decltype() deduces the type of expression. In the example, it defines the y variable so that it's the same type as a. As you can imagine, this would not be possible with auto. Why? This is pretty simple: decltype() tells the compiler to define a variable of a specific type; in the first example, y is a variable with the same type as a. With auto, the type is deduced automatically.

We should use auto and decltype() anytime we don't have to explicitly specify the type of a variable; for example, when we need a double type (and not a float). It's worth mentioning that both auto and decltype() deduct types of expressions that are already known to the compiler, so they are not runtime mechanisms.

There's more...

There is a specific case that must be mentioned. When auto uses {} (uniform initializers) for type deduction, it can cause some headaches (or at least behaviors that we wouldn't expect). Let's look at an example:

auto fuelLevel {0, 1, 2, 3, 4, 5};

In this case, the type that's being deduced is initializer_list<T> and not an array of integers, as we could expect.

See also

The books Effective Modern C++ by Scott Meyers and The C++ Programming Language by Bjarne Stroustrup cover these topics in great detail.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
C++ System Programming Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon