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.

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.