Modern C++ › C++17
CTAD, Fold Expressions and Inline Variables
Template deduction for classes and variadic folds.
Explanation
Three C++17 features that remove template boilerplate.
CTAD (class template argument deduction): the compiler deduces the template arguments of a class from its constructor arguments. std::pair p(1, 2.5); is pair<int,double>, std::vector v{1, 2, 3}; is vector<int>. For cases it cannot guess, you add a deduction guide: Box(const char*) -> Box<std::string>;.
Fold expressions reduce a parameter pack with an operator in one line. With op as any binary operator:
| Form | Expands to |
|---|---|
(pack op ...) |
right fold: p1 op (p2 op p3) |
(... op pack) |
left fold: (p1 op p2) op p3 |
(init op ... op pack) |
binary folds with an initial value |
Over the comma operator, a fold executes an action for each argument. For &&, || and , an empty pack is allowed (true, false, void()).
Inline variables: inline on a variable permits its definition in a header included by many translation units (one entity, no ODR violation). static inline data members need no separate definition in a .cpp.
Example
#include <iostream>
#include <mutex>
#include <string>
#include <utility>
#include <vector>
template <typename T>
struct Box {
T value;
Box(T v) : value(std::move(v)) {}
};
Box(const char*) -> Box<std::string>; // deduction guide
template <typename... Ts>
auto sum(Ts... xs) { return (xs + ...); } // unary right fold
template <typename... Ts>
bool allPositive(Ts... xs) { return ((xs > 0) && ...); } // fold over &&
template <typename... Ts>
void printAll(const Ts&... xs) {
((std::cout << xs << ' '), ...); // fold over the comma operator
std::cout << '\n';
}
template <typename... Ts>
auto sumPlus(int init, Ts... xs) { return (init + ... + xs); } // binary fold with an initial value
struct Config {
static inline int counter = 0; // C++17: no definition in a .cpp needed
static inline const std::string name = "app";
};
inline constexpr double kPi = 3.14159; // header-safe constant
int main() {
std::pair p(1, 2.5); // pair<int, double>
std::vector v{1, 2, 3}; // vector<int>
std::mutex m;
std::lock_guard lock(m); // lock_guard<std::mutex>
Box b1(42); // Box<int>
Box b2("hello"); // Box<std::string> via the guide
std::cout << p.first << v.size() << b1.value << b2.value << "\n";
std::cout << sum(1, 2, 3, 4) << " " << allPositive(1, 2, -3) << " " << sumPlus(100, 1, 2) << "\n";
printAll("x =", 10, 3.5);
++Config::counter;
std::cout << Config::counter << Config::name << kPi << "\n";
return 0;
}
Common pitfalls
- CTAD can pick the wrong type:
std::vector v{3, 7}makes a 2-elementvector<int>;std::vector v(3, 7)makes three 7s. A copy-likestd::vector w{v}copies instead of nesting. sum()with an empty pack is an error for+folds; use a binary fold with an initial value.- Operator precedence in folds: always put the whole fold in parentheses.
- CTAD with
const char*deducesconst char*, notstd::string, unless you add a guide. inlineon a variable is unrelated to inlining code: it only permits multiple identical definitions.
Legacy vs modern
// C++11: recursion with a base case
template <typename T> T sum(T x) { return x; }
template <typename T, typename... R> T sum(T x, R... r) { return x + sum(r...); }
// C++17
template <typename... Ts> auto sum(Ts... xs) { return (xs + ...); }
Before CTAD you wrote std::make_pair, std::make_tuple and std::make_unique-style helpers, or repeated the type (std::pair<int, double> p(1, 2.5);). Before inline variables, a header constant used static const (one copy per file) or a definition in a single .cpp.
Exercises
1. Write countTrue(bool...) with a fold that counts how many arguments are true.
template <typename... Bs>
int countTrue(Bs... bs) { return (0 + ... + static_cast<int>(bs)); }