• Complain

Gennaro - Advanced Metaprogramming in Classic C++

Here you can read online Gennaro - Advanced Metaprogramming in Classic C++ full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. City: Berkeley;CA, year: 2015, publisher: Apress, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Gennaro Advanced Metaprogramming in Classic C++
  • Book:
    Advanced Metaprogramming in Classic C++
  • Author:
  • Publisher:
    Apress
  • Genre:
  • Year:
    2015
  • City:
    Berkeley;CA
  • Rating:
    5 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 100
    • 1
    • 2
    • 3
    • 4
    • 5

Advanced Metaprogramming in Classic C++: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Advanced Metaprogramming in Classic C++" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Advanced Metaprogramming in Classic C++ aims to be both an introduction and a reference to C++ template metaprogramming (TMP); TMP is presented in the book as a set of techniques that will bring a new style in C++ and make code exceptionally clear and efficient. The book deals with language aspects, design patterns, examples and applications (seen as case studies). Special emphasis is put on small reusable techniques that will improve the quality of daily work. What makes the book exceptional is the level of understanding of the concepts involved imparted by the author. This is not just a rote overview of metaprogramming. You will truly understand difficult topics like static assertions, how to write metafunctions, overload resolution, lambda expressions, and many others. More than that, you will work through them with practical examples guided by the authors frank explanations. This book requires you to think and to learn and to understand the language so that you can program at a higher level.

Gennaro: author's other books


Who wrote Advanced Metaprogramming in Classic C++? Find out the surname, the name of the author of the book and a list of all author's works by series.

Advanced Metaprogramming in Classic C++ — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Advanced Metaprogramming in Classic C++" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Part 1
#include
Davide Di Gennaro 2015
Davide Di Gennaro Advanced Metaprogramming in Classic C++ 10.1007/978-1-4842-1010-9_1
1. Templates
Davide Di Gennaro 1
(1)
London, United Kingdom
C++ supports a variety of styles.
Bjarne Stroustrup, A Perspective on ISO C++
Programming is the process of teaching something to a computer by talking to the machine in one of its common languages. The closer to the machine idiom you go, the less natural the words become.
Each language carries its own expressive power. For any given concept, there is a language where its description is simpler, more concise, and more detailed. In assembler, we have to give an extremely rich and precise description for any (possibly simple) algorithm, and this makes it very hard to read back. On the other hand, the beauty of C++ is that, while being close enough to the machine language, the language carries enough instruments to enrich itself.
C++ allows programmers to express the same concept with different styles and good C++ looks more natural.
First you are going to see the connection between the templates and the style, and then you will dig into the details of the C++ template system.
Given this C++ fragment:
double x = sq(3.14);
Can you guess what sq is? It could be a macro:
#define sq(x) ((x)*(x))
A function:
double sq(double x)
{
return x*x;
}
A function template:
template
inline scalar_t sq(const scalar_t& x)
{
return x*x;
}
A type (an unnamed instance of a class that decays to a double):
class sq
{
double s_;
public:
sq(double x)
: s_(x*x)
{}
operator double() const
{ return s_; }
};
A global object:
class sq_t
{
public:
typedef double value_type;
value_type operator()(double x) const
{
return x*x;
}
};
const sq_t sq = sq_t();
Regardless of how sq(3.14) is implemented, most humans can guess what sq(3.14) does just looking at it. However, visual equivalence does not imply interchangeableness . If sq is a class, for example, passing a square to a function template will trigger an unexpected argument deduction:
template void f(T x);
f(cos(3.14)); // instantiates f
f(sq(3.14)); // instantiates f. counterintuitive?
Furthermore, you would expect every possible numeric type to be squared as efficiently as possible, but different implementations may perform differently in different situations:
std::vector v;
std::transform(v.begin(), v.end(), v.begin(), sq);
If you need to transform a sequence, most compilers will get a performance boost from the last implementation of sq (and an error if sq is a macro).
The purpose of TMP is to write code that is:
  • Visually clear to human users so that nobody needs to look underneath.
  • Efficient in most/all situations from the point of view of the compiler.
  • Self-adapting to the rest of the program.
Self-adapting means portable (independent of any particular compiler) and not imposing constraints. An implementation of sq that requires its argument to derive from some abstract base class would not qualify as self-adapting.
The true power of C++ templates is style . Compare the following equivalent lines:
double x1 = (-b + sqrt(b*b-4*a*c))/(2*a);
double x2 = (-b + sqrt(sq(b)-4*a*c))/(2*a);
All template argument computations and deductions are performed at compile time, so they impose no runtime overhead. If the function sq is properly written, line 2 is at least as efficient as line 1 and easier to read at the same time.
Using sq is elegant:
  • It makes code readable or self-evident
  • It carries no speed penalty
  • It leaves the program open to future optimizations
In fact, after the concept of squaring has been isolated from plain multiplication, you can easily plug in specializations:
template
inline scalar_t sq(const scalar_t& x)
{
return x*x;
}
template <>
inline double sq(const double& x)
{
// here, use any special algorithm you have!
}
1.1 C++ Templates
The classic C++ language admits two basic types of templates function templates and class templates :
Here is a function template:
template
scalar_t sq(const scalar_t& x)
{
return x*x;
}
Here is a class template:
template
<
typename scalar_t, // type parameter
bool EXTRA_PRECISION = false, // bool parameter with default value
typename promotion_t = scalar_t // type parameter with default value
>
class sum
{
// ...
};
When you supply suitable values to all its parameters, a template generates entities during compilation. A function template will produce functions and a class template will produce classes. The most important ideas from the TMP viewpoint can be summarized as follows:
  • You can exploit class templates to perform computations at compile time.
  • Function templates can auto-deduce their parameters from arguments. If you call sq(3.14) , the compiler will automatically figure out that scalar_t is double , generate the function sq , and insert it at the call site.
Both kinds of template entities start declaring a parameter list in angle brackets. Parameters can include types (declared with the keyword typename or class ) and non-types: integers and pointers.
Note that, when the parameter list is long or when you simply want to comment each parameter separately, you may want to indent it as if it were a block of code within curly brackets.
Parameters can in fact have a default value:
sum S1; // template argument is 'double', EXTRA_PRECISION is false
sum S2;
A template can be seen as a metafunction that maps a tuple of parameters to a function or a class. For example, the sq template
template
scalar_t sq(const scalar_t& x);
maps a type T to a function:
T Picture 1 T (*)(const T&)
In other words, sq is a function with signature double (*)(const double&) . Note that double is the value of the parameter scalar_t .
Conversely, the class template
template
class basic_string;
maps a type T to a class:
T Picture 2 basic_string
With classes, explicit specialization can limit the domain of the metafunction. You have a general template and then some specializations; each of these may or may not have a body.
// the following template can be instantiated
// only on char and wchar_t
template
class basic_string;
// note: no body
template < >
class basic_string
{ ... };
template < >
class basic_string
{ ... };
char_t and scalar_t are called template parameters . When basic_string and sq are used, char and double are called template arguments , even if there may be some confusion between double (the template argument of sq ) and x (the argument of the function sq ).
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Advanced Metaprogramming in Classic C++»

Look at similar books to Advanced Metaprogramming in Classic C++. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Advanced Metaprogramming in Classic C++»

Discussion, reviews of the book Advanced Metaprogramming in Classic C++ and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.