June 2017
Intermediate to advanced
532 pages
12h 59m
English
What we just learned about was implicit template type deduction. In some cases, we cannot rely on implicit type deduction. Consider the following example class:
template <typename T>struct sum { T value; template <typename ... Ts> sum(Ts&& ... values) : value{(values + ...)} {}};
This struct, sum, accepts an arbitrary number of parameters and adds them together using a fold expression (have a look at the fold expression recipe a little later in this chapter to get more details on fold expressions). The resulting sum is saved in the member variable value. Now the question is, what type is T? If we don't want to specify it explicitly, it surely needs to depend on the types of the values provided in the constructor. If we provide ...
Read now
Unlock full access