Priya
Priya

Reputation: 77

why the output of the auto variable displays something not related to type?

I tried a example on Auto for variable initialization and STL in C++. For normal variable, type was printed using : typeid(var_name).name() to print i (integer) / d(float) / pi(pointer) which works fine. But while working on STL,

`#include <iostream>
 #include <vector> 
 using namespace std;

 int main() 
 { 
    vector<string> st; 
    st.push_back("geeks");
    st.push_back("for"); 

    for (auto it = st.begin(); it != st.end(); it++) 
        cout << typeid(it).name() << "\n"; 

    return 0; 
} 
`
which gives output like,
`N9__gnu_cxx17__normal_iteratorIPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIS6_SaIS6_EEEE
N9__gnu_cxx17__normal_iteratorIPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIS6_SaIS6_EEEE`  

and I am unable to understand the output logic behind it, can anyone explain why it is giving output like this? and thanks in advance

Upvotes: 1

Views: 2452

Answers (1)

Stu
Stu

Reputation: 341

That's the "name mangled" version of the name of the type of it. typeinfo::name() is not required by the standard to return a name in human-readable format (a shortcoming IMHO) and GCC doesn't do so.

To get the actual, human-readable name, you need to call the abi::__cxa_demangle() function provided by GCC, but note that this is non-portable so if your project needs to work on different compilers you'll need to wrap it appropriately.

Upvotes: 1

Related Questions