Modern C++ : The Actual Type

Well, the other day I stumbled onto  this which in turn lead me to this .  Which in turn made me realize I still have not published my little useful utility. Addition to my modern c++, so called dbj++ library or so called “foundation mechanisms” . To be used in lieu with the ubiquitous modern c++ std lib. 

So.  “Familiar template syntax for generic lambdas” (aka Document #: P0428R2) is one very clean text.   Motivation, point 2 explains what is the issue with the current state (in c++17) qith generic lambdas.

It is the decltype and auto arguments, dancing together.

Above the decltype usage, faithfully presents the reference and const qualifiers too. Which in turn (as article shows, so I won’t repeat it here) make further usage of arg_type surprising, which is not a good thing.

Also. If you call above lambda with a pointer, decltype will of  course use that too. Article suggests using std::decay. But alas, this will not reveal the fact that argument sent might be for example a pointer. Yes, I know there is std::is_pointer , but  do we have to use it each time?

Thus, I have developed this little utility I am calling the dbj::actual_type, it is in essence a type helper that reveals the “actual type” of the result of decltype call.

Yes it also can be used to find out if the type is a pointer or double pointer, too.

The basic form of a usage:

Now we have the actual type of the generic argument to the generic lambda. My utility for using auto types on generic lambdas.

The bonus discussion

Eagle eyed readers might have learned here, not to adorn auto arguments,  as const or  const & or const &&, or simillar.

Now imagine calling the above with lets say char ** type argument? The surprise_ahead type will be const reference to double pointer.  I doubt that is what you want in normal circumstances. My advice: just declare generic lambda arguments as auto arg, and leave it to modern c++ move semantics to sort out the copy elision.

And yes, pay attention to the actual type.

Leave a Reply