Skip to content

Latest commit

 

History

History
110 lines (84 loc) · 1.24 KB

template_stuff.md

File metadata and controls

110 lines (84 loc) · 1.24 KB

template<auto>

C++14 C++17
template <typename T, T v>
struct integral_constant
{
   static constexpr T value = v;
};
integral_constant<int, 2048>::value
integral_constant<char, 'a'>::value
template <auto v>
struct integral_constant
{
   static constexpr auto value = v;
};
integral_constant<2048>::value
integral_constant<'a'>::value

Fold Expressions

How do you write `sum()` ?
auto x = sum(5, 8);
auto y = sum(a, b, 17, 3.14, etc);
C++14 C++17
auto sum() { return 0; }

template <typename T>
auto sum(T&& t) { return t; }

template <typename T, typename... Rest>
auto sum(T&& t, Rest&&... r) {
   return t + sum(std::forward<Rest>(r)...);
}



template <typename... Args>
auto sum(Args&&... args) {
   return (args + ... + 0);
}