A foliage of folly

Can we circumvent the private access rules without relying on techniques that are considered “arcane” and likely to be made illegal (at some point)?

I recently read the article Profiting from the Folly of Others by Alastair Harrison, which walks through a technique to circumvent private access rules, particularly by means of (ab)using the template instantiation mechanism together with injections of inline friend definitions. I first saw this technique used in Filip Roséen’s blog article series on stateful metaprogramming back in 2015, which made use of it to implement a constexpr-counter that were, if not fully, at the very least nearly1 standard compliant. A. Harrison’s article mentions that the origin of the technique is likely two blog articles by Johannes Schaub published in 2010-20112.

A typedef for when an alias declaration cannot

Tags// , , ,

What is the difference between typedef declarations and alias declarations?

A common question, particularly for developers migrating from pre-C++11 projects to more modern C++, is whether there are any differences between typedef declarations and alias declarations.

// target_specific_fixed_width_types.h
typedef float Float32_t;   // typedef declaration
using Float64_t = double;  // alias declaration

Same entity, different type?

Tags// , ,

Is(/are) there any scenario(s) where a name, which refers to an entity, actually refers to a different types (whilst still referring to the same entity) depending on context and/or scope? Or, to put it in another way, is(/are) there any scenario(s) where the type of a named entity changes depending on context and/or scope?

The fickle aggregate

What is an aggregate class?

Aggregate class types make up a special family of class types that can be, particularly, initialized by means of aggregate initialization, using direct-list-init or copy-list-init, T aggr_obj{arg1, arg2, ...} and T aggr_obj = {arg1, arg2, ...}, respectively.

Leveraging non-deduced contexts for template argument deduction

Tags// , ,

What is an example of a non-deduced context, and why can it be useful to know about these?

The following snippet

template<typename T>
struct Foo { T t; };

template<typename T>
void addToFoo(Foo<T>& foo, T val) { foo.t += val; }

int main() {
    Foo<long> f{42};
    addToFoo(f, 13); // error: no matching function for call to 'addToFoo'
    return 0;
}

is ill-formed, as (function) template argument deduction for the dependent function parameters foo and val of the addToFoo function template resolves to different, conflicting types in the addToFoo(f, 13) function call.