C++

Parameterized Functions

I was recently playing around with Perl 6, where I encountered dynamic variables, which are looked up in the dynamic scope at runtime. This means that it is possible to shadow variables on the call stack instead of through lexical scoping. When I was thinking about the implications of that concept, I realized that it should be possible to use it to parameterize functions without explicitly passing arguments. Some languages supply similar concepts explicitly.

A Tour of Rare C++ Features

Welcome to the overview page for my “tour of rare C++ features”. Please remember that there are good reasons why many of the contortions described herein are vanishingly rare in real software! The tour revolves around the snippet of C++ below, which demonstrates rather handily why some people claim that C++ is a bad language - however, I aim to show the reasoning behind and usefulness of each, or at least most, of these language features.

A Tour of Rare C++ Features Part 7: The Comma and Ternary Operators

In the previous installment of this series we dealt with if statements, function style casts and grouping digits of numbers. This time, our focus will lie on the only remaining two statements of our guiding example. By now, most of the guiding example of this series should be pretty well known, with the only parts that may still be somewhat enigmatic being highlighted below: /* This is valid C++ */ auto main() - decltype('O.

Fully Qualified Out-of-Line Definitions

Recently I stumbled upon a piece of C++ code that fails to compile with Clang, although it works with gcc just fine. Since I was unable to divine at a glance what was going on, I created the following Minimal Complete Verifiable Example: using test_t = int; struct S { test_t f(); }; // The next line will error out on clang with: // 'test_t' (aka 'int') is not a class, namespace, or enumeration ::test_t ::S::f() { return 0; } Only three lines, and all of them are “obviously correct”.

A Tour of Rare C++ Features Part 6: Declaring Variables in If Statements, Function Style Casts and Digit Grouping

In the previous installment of this series we dealt with the function declaration from hell. This post will focus on the if/else that follows it in the example that guides us through this series. For your convenience, I have highlighted the only parts of the example that remain completely undiscussed as of now: /* 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.

A Tour of Rare C++ Features Part 5: The Function Declaration from Hell

In the previous installment we discovered the ancient Digraphs and Trigraphs, as well as the more modern lambda expressions. This post focuses on the type from hell, which is overly complicated, but still full of fun. The guiding example of this series is still the same and should now start to make some sense if you have read the previous posts in this series: /* This is valid C++ */ auto main() - decltype('O.

Comprehensive C++ Lambda Overview

This article is intended to be a comprehensive but still understandable and beginner-friendly overview over lambda functions in C++. Any section marked as “advanced” can be safely skipped, although they should be readable enough to understand for intermediate C++ developers as well. Please be aware that most of these features require at least C++11 support (g++ and clang++ activate it via the -std=c++11 argument) and generalized lambda captures even require C++14 support (g++ and clang++ activate it via the -std=c++14 argument).

A Tour of Rare C++ Features Part 4: Generalized Lambda Captures, Binary Literals and URLs

In the previous installment we discovered the ancient Digraphs and Trigraphs, as well as the more modern lambda expressions. We did however leave out what exactly the capture list that is used in the guiding example of this series means, which is the first topic of this post. Afterwards we will spend a tiny bit of time on how there can be an URL directly in the code. The guiding example of this series is still the same and should now start to make a small bit of sense if you have read the previous posts in this series:

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: