vendredi 21 octobre 2022

How can I print the exact type of variables in c++? [duplicate]

What I know:

#include <iostream>
#include <typeinfo>
using namespace std;

int main()  
{    
    int a = 3;
    const int& b = 4;
    cout<<typeid(a).name()<<endl;
    cout<<typeid(b).name()<<endl;
}

So this 👆🏽 will going to print:

i    // i stands for int
i    

But my question is what can I do to print the exact type of b?

This is what I tried :

#include <iostream>
#include <typeinfo>
using namespace std;

int main()  
{    
    int a = 3;
    const int& b = 4;
    cout<<typeid(a).name()<<endl;
    cout<<typeid(b).name()<<endl;
    
    decltype(b) k = 4;
    cout<<typeid(k).name()<<endl;
    return 0;
}

but still, output is :

i
i
i

Aucun commentaire:

Enregistrer un commentaire