An Easy Jump-Start in Real-Time C++

Developers new to real-time C++ may want to obtain some useful results quickly before taking the time to master all the intricate details of the C++ language. This chapter addresses this desire by presenting a simple, yet effective, subset of the C++ lang

  • PDF / 203,051 Bytes
  • 13 Pages / 439.36 x 666.15 pts Page_size
  • 75 Downloads / 175 Views

DOWNLOAD

REPORT


An Easy Jump-Start in Real-Time C++

Developers new to real-time C++ may want to obtain some useful results quickly before taking the time to master all the intricate details of the C++ language. This chapter addresses this desire by presenting a simple, yet effective, subset of the C++ language specifically designed for those seeking a lightweight and reliable jump-start in real-time C++. The C++ subset in this chapter represents a judicious selection of some of the most easy-to-do things in C++ that can potentially be used in the widest possible range of programming situations. The strategy of this C++ subset is shown in Fig. 3.1.

3.1 Declare Locals when Used In C++, local variables can be declared where they are first used. They do not necessarily need to be bound to the opening curly brace of a scope. This can improve code readability and facilitate compiler optimization. Fig. 3.1 The sketch shows how a small subset of C++ can potentially be used for a wide variety of programming situations

Everything I Want To Do in Real-Time C++

All of C++

C++ Subset

Where the Subset Can Potentially Be Used

C.M. Kormanyos, Real-Time C++, DOI 10.1007/978-3-642-34688-0__3, © Springer-Verlag Berlin Heidelberg 2013

33

34

3 An Easy Jump-Start in Real-Time C++

The code below, for example, conveniently declares integral variables i, j and k near where they are first used in the subroutine. void void void void

initialize(); use_i(const int); use_j(const int); use_k(const int);

void do_something() { // Initialize someting. initialize();

// Declare i when using it in use_i(). const int i = 3; use_i(i); // Declare j when using it in use_j(). const int j = 7; use_j(j); // Declare k in the scope of the for-loop. for(int k = 0; k < 10; ++j) { use_k(k); } }

3.2 Fixed-Size Integer Types The C++ standard library has a complete set of portable fixed-size integer types in its library. User-defined types can potentially be clumsy and hard-to-maintain. They can be replaced with standard fixed-size integer types such as std::uint8_t, std::uint16_t, etc. Fixed-size integer types were first introduced briefly in Sect. 1.8. The code below, for instance, uses variables that have different kinds of fixedsizes with 8, 16 and 32 bits. #include

// This has exactly 16-bits. constexpr std::uint16_t value_exact_16 = 0xABCDU;

3.3

The bool Type

35

// This has at least 32-bits. constexpr std::uint_least32_t value_at_least32 = 0xFFFFFFFFUL; void do_something() { // Use the fastest integer with at least 8-bits. for(std::uint_fast8_t i = 0U; i < 10U; ++i) { // Do something with i. // ... } }

3.3 The bool Type C++ includes a built-in Boolean type bool that has two and only two possible values, true and false. In C++, the result of a Boolean test has the type bool and its value is either true or false. Using C++’s built-in Boolean type can improve the clarity of logic and simplify coding. The code below, for instance, uses C++’s built-in Boolean type bool in logical statements. bool valid(); bool login(); void start_session(); void do_somethin