How to view the deduced types

How to view the deduced types

C++ in a new way

·

3 min read

continuing the series of advanced c++, we came to a track where we can see all the set rules of deducing type in reality and should I remember every single rule of the deducing type, well the answer you want is no, but actually yes, in short terms I am going to explain how in practice you see the type of entities. we have two ways to view the deduction of objects based on the runtime or compilation time.

IDE editors

code editors often show the types of program entities, when you hover your cursor over the entity. so it is important to have an offline IDE.

const int a=2;
auto x=a; // x is int
auto y=&a; // y is const int*

if you hover over the above code, remember the const-ness of the vars is lost. but to make sure the editor shows you the deduced type your code should make sense for the compiler. second, it is sometimes useless in more complex examples.

Compiler Diagnostics

this is a little tricky, errors are meant to bother our peaceful mind with nosing and dangling our code, but now it is time to laugh at the compiler to tell us what we need based on the error message.

template<typename T>. //declaration for TD only.
class TD;            // TD=="type display"
// the compiler will realse an error message cause there is no template to instanite.
TD<decltype(x)> xType;  
TD<decltype(y)> ytype;

// use the variableNameType beacuse the output messgae is 
error: aggregate 'TD<int> xType' has incomplete type and cannot be defined
error: aggregate 'TD<const int *> yType' has incomplete type and cannot be defined

Runtime Output

this one relies on the fact of using display approach. to be honest it is not my favorite cause it is the textual representation is not the best.

std::cout << typeid(x).name() << '\n'; // display types for
std::cout << typeid(y).name() << '\n'; // x and y

using such an approach doesn't guarantee the best output sometimes refers to int as "i" and the pointer to const as "pk"

complex examples.

let's see how the output approach will handle the problem of more complex examples oh damn I forgot to put the example

template<typename T>
void F(const T&param);
std::vector<int>creatvect();
const auto vi=creatvect();
if(!vi.empty()){
f(&vi[i]);
}

if we used the same output format the result is ambiguous, T= PK6int, param= PK6int. this result is both ambitious and inaccurate, first of all, what does it mean- PK stands for the pointer to const and 6 refers to the length of the class name and int is int really? the result is wrong cause the T is const int and the param is const int&. so sad to tell you that the std::type_info::name is not reliable, as we move deeper to the implementation of type_info::name we will find out the argument is passed by value, and from the first article- go and read it. First, the type’s reference-ness is removed, and then the constness of the resulting pointer is eliminated. and many other problems but what is the solution?

the solution to all the above problems

it is easy to say so. and here we go, TypeIndex lib, or boost: type index is designed to succeed. it is available at boost.com.

   #include <boost/type_index.hpp>

   template<typename T>
   void f(const T& param)
   {
     using std::cout;
     using boost::typeindex::type_id_with_cvr;
// show T
cout << "T = "
<< type_id_with_cvr<T>().pretty_name() << '\n';
     // show param's type
     cout << "param = "
          << type_id_with_cvr<decltype(param)>().pretty_name()
<< '\n'; ...
}

Things to remember

  1. Deduced types can often be seen using IDE editors, compiler error messages, and the Boost TypeIndex library.

  2. The results of some tools may be neither helpful nor accurate, so an under‐ standing of C++’s type deduction rules remains essential.