c++ Reduce compound to base type

You are advancing.  Things are becoming clearer and clearer. By now you feel confident enough to tackle head on the c++ meta-programing, perhaps? Compile time shenanigans and the rest.

(Wandbox is here)

One tool seems very powerful: std::is_same<T1, T2> . Using this, one can code compile time “magic” based on equality of two types.  For example.


But. This never seems to work as expected. And the chief reason for this kind of problems? Consider this:

template< typename T> 
void function( T t_ ) {
 // what is the type T in here?

Somehow,  T is almost never a type you expect it to be. And you have probably not read the fundamentals of standard C++ before starting this journey on “the path peppered with a glass”? :) To answer this simple question “what is the type of T”, one needs to know what exactly type T can be. In standard C++.

And speaking of types we immediately bump into SFINAE. SFINAE is one thing almost every C++ beginner put’s under the carpet as long as possible. Until it has to be understood and used.

The  core to success here is a complete understanding of the  classification of types in standard C++

  1. Compound types
    Array, function, object pointer, function pointer, member object pointer, member function pointer, reference, class, union, or enumeration, including any cv-qualified variants.
  2. Fundamental types
    Arithmetic type (integral type or a floating-point type), void, or nullptr_t. Where the integral types are:  bool, char, char16_t, char32_t, wchar_t, short, int, long, long long, or any implementation-defined extended integer types, including any signed, unsigned, and cv-qualified variants

Huh? Yes, it is as simple as that, I am afraid.

So you bravely dive into standard C++ types taxonomy, and you quickly develop the habit to use standard and useful <type_traits> to fight it out with the types. And it seems so simple and logical when looking into examples online. But not so when you try it.

To help myself out ( and using C++17 and std::  goodies), I have developed a few useful template aliases, for taming this uncertainty situation around the “What is the type of  T” question.

Here I will present a tool that will reduce ‘anything’ to the base type.   Please look into the classification above now. So.  A tool that will reduce any compound type to the base fundamental type. Its most canonical usage:

“Base type” is not a fundamental type. The base type is a type which is a base of a compound type.  To clarify this completely let me show you some hard testing first. Here we use several legal C++ compound types, invented for testing only.

As you can see this really works. Whatever compound type you give, it will be reduced to its base type.

This tool will also work for double, triple, etc pointers. And here is the code:

Your problem function from above will have to be changed to use my tool to work.

Feel free to copy paste and use. Just please leave in the copyright statement.

Obligatory Wandbox is here.