Stunt programming. Modern C++ is not about that.

Option Two

Lambdas! Yes. That divides modern C++ man,  from old C++ boys. We remember the factory pattern and we will use it here too.  Thus showing the unmistakable OO pedigree, too.

Looks much more in line with our status. Does it not? Very clever little stunt this is.

The real modern C++ thinking shines through. Functional programming and the rest.  Function pointers are nicely (and obediently by compiler) made. auto is used properly so there are no ugly  pointers to instances of template functions. Or was it function templates?

Few little problems here too. First this pesky default argument issue. It seems the good old  argument as “pointer to null” works the best as default argument idiom. It seems it is far from modern C++. We try the “modern” solution on that line:
return [&](const T & new_ = defval_) { ... } 

But alas no can do:

error C2587: 'defval_': illegal use of local variable as default parameter

Oh, OK. That is a bit of a surprise.  We quickly “Google out” for several advice’s but it seems nothing works. Time passes by.  Fine. Is this really a problem?

Well yes it is. If we pass the pointer we need to guarantee it points into the meaningful value. This line is especially troublesome:

default_value = *new_;

We make two big assumptions here: first that de-referencing will produce a meaningful value of type T, and second that operator “=” exists and can take the right hand side as we wish it to.

Hmm. Not entirely sure here. Should we try hard and pull some latest modern C++ tricks here? C++17 guides? Or some yet undiscovered nugget from the stdlib.

And yes we also have noticed this solution does not work as we expect it to. It simply does not return the default value on the first call? Hm. Hm.

Ok, let me jump in here and help to our unfortunate programmer and first provide an C++17 solution. With errors in logic removed too.

Yes this is a real modern C++17. std::optional<> and a such. And it works too.  So what could be possibly wrong with it?

The reality

  • Your team/company is still on C++11, or is it C++98?
    • std::optional (at least ) requires C++17
  • This code is pretty hard to understand to your team
    • yes this is clever but definitely on the edge of “stunt”
  • To maintain or expand this code  authors presence is required
  • Here insert your favorite show stopper  …

What should we do? We should stop here and think. How would we actually implement this if we focus on the requirements instead  of the latest features possible with the programming language we use. After all if something is in there , in the modern C++, that does not mean we have to use it.

Imperative (in case you forgot) is to solve the requirements. In a feasible manner.