For a given class, one needs sometimes to trace calls to constructors/destructor.
An obvious way is to put some traces in these methods but it is often tedious and intrusive for the class to be instrumented. A possible way would be to inherit from some TraceConstructor struct:
#include <iostream> template<auto& OUTPUT> struct TraceConstructor { TraceConstructor () { OUTPUT << "default constructor\n"; }; ~TraceConstructor () { OUTPUT << "destructor\n"; }; TraceConstructor ([[maybe_unused]] TraceConstructor const& o) { OUTPUT << "copy constructor\n"; }; TraceConstructor ([[maybe_unused]] TraceConstructor && o) { OUTPUT << "move constructor\n"; }; }; struct A : TraceConstructor<std::cout> { int n_; A(int n) : n_(n) {} }; int main() { A a1 {4}; A a2 = a1; A a3 = std::move(a1); } Question: Instead of reinventing the wheel, is there some idiomatic feature like this (in std, boost, other...) and that is more complete than this naive (and probably incomplete) approach ?
Note: I just focus here on the constructors/destructor here but obviously one could consider assignment operator as well.
TraceConstructorand inheriting from it just as you've done is the idiomatic way I'd do it.