Fold Expressions

While implementing a logging facility I recently stumbled across a problem that can be reduced to the following: Inside a variadic function template, if the log level is high enough, forward all arguments into an ::std::ostream&. Dutifully I solved it in a recursive fashion: inline log(loglevel_t min_level) { } template<typename T, typename... V> void log(loglevel_t min_level, T&& arg, V&& more_args) { if(min_level <= current_level) { out << ::std::forward<T>(arg); log(min_level, ::std::forward<T>(more_args); } } However, not only does that feel like a clutch - after all I really just want to push everything into the output stream - but it also requires the instantiation of all those additional functions, which causes the executable to grow larger than necessary1.

A Tour of Rare C++ Features Part 3: Digraphs, Trigraphs and Lambda Functions

In the previous installment we explored function try blocks, catch-all exception handlers and the correct way to rethrow exceptions. This time, we are going to talk about ancient and fairly modern features: Digraphs have been around for so long that the reason for their existence might be surprising, while lambda functions are one of the big new features in C++11, which have been further refined with C++14. The example for this post is still the same one as in the previous ones and should now start to make a tiny bit of sense if you have read the previous posts in this series:

A Tour of Rare C++ Features Part 2: Function Try Blocks, Catch-All Exception Handlers and Rethrowing

In the previous installment of this series we explored the combined use of trailing return types, decltype and multicharacter literals that together explain why the following snippet actually contains a correct main function: /* This is valid C++ */ auto main() -> decltype('O.o') try <%[O_O = 0b0]<% https://gha.st/a-tour-of-rare-cpp-features/ typedef struct o O; o*(*((&&o(o*o))<:'o':>))(o*o); if(O*O = decltype(0'0[o(0)](0))(0)) 1,000.00; else return 0==O==0 ? throw O_O : O_O; %>();%> catch(...) { throw; } This time, we are going to explore how the body of this function can be correct.

A Tour of Rare C++ Features Part 1: Trailing Return Types, decltype and Multicharacter Literals

C++ is a treasure trove of entertaining, fascinating and just plain weird features. While most of them have sound reasons, rules like that “main may not be called by the program” feel rather arbitrary. Add to that, that there is a lot of rules and features, and some of them will fall through the cracks. As there are very real reasons not to use many of the features I am going to present, I have made a small example that demonstrates the result of what can happen if you enjoy this kind of activity too much: