Domanda

C'è un compilatore là fuori che restituisce il nome di un tipo in modo leggibile (o biblioteca che fornisce tale funzionalità o strumento).Essenzialmente ciò che voglio è la stringa corrispondente all'espressione del tipo scriverlo nel tuo codice sorgente.

È stato utile?

Soluzione

 typeid(var).name()
.

è quello che stai cercando.L'uscita differisce dal compilatore al compilatore però ... Per gcc L'uscita per int è i, per unsigned è j, ad esempio.Ecco un piccolo programma di test:

#include <iostream>
#include <typeinfo>

struct A { virtual ~A() { } };
struct B : A { };

class C { };
class D : public C { };

int main() {
  B b;
  A* ap = &b;
  A& ar = b;
  std::cout << "ap: " << typeid(*ap).name() << std::endl;
  std::cout << "ar: " << typeid(ar).name() << std::endl;

  D d;
  C* cp = &d;
  C& cr = d;
  std::cout << "cp: " << typeid(*cp).name() << std::endl;
  std::cout << "cr: " << typeid(cr).name() << std::endl;

  int e;
  unsigned f;
  char g;
  float h;
  double i;
  std::cout << "int:\t" << typeid(e).name() << std::endl;
  std::cout << "unsigned:\t" << typeid(f).name() << std::endl;
  std::cout << "char:\t" << typeid(g).name() << std::endl;
  std::cout << "float:\t" << typeid(h).name() << std::endl;
  std::cout << "double:\t" << typeid(i).name() << std::endl;
}
.

Vedi anche questa domanda: TypeID (T) .name () Alternative in C ++ 11?

Altri suggerimenti

Sotto è una piccola classe che scrivo per controllare il tipo.Puoi aggiungere più tipo ad esso se necessario.Non può controllare i tipi di array, la mappa o qualsiasi cosa sarebbe troppo avanzata.È possibile aggiungere altri tipi se necessario o addirittura aggiungere il tuo tipo personalizzato.È solo un piccolo campione.

Inoltre, la funzione GetTyPe è ciò che stai cercando.Sulla base di quale tipo è abbinato, è possibile personalizzare la stringa di uscita.Ho omesso la parte include, quindi per quello che devi solo trovare il giusto includono file come questa libreria è una parte della mia biblioteca più grande.

/ * header.hpp * /

class _cType
{
    public:
    _cType();
    ~_cType();

    template<class T, class U>
    static bool isSame(T &vl, U &vr);

    template<class T>
    static bool isInt(T &v);

    template<class T>
    static bool isUInt(T &v);           

    template<class T>
    static bool isFloat(T &v);

    template<class T>
    static bool isLong(T &v);

    template<class T>
    static bool isDouble(T &v);

    template<class T>
    static bool isBool(T &v);

    template<class T>
    static bool isString(T &v);

    template<class T>
    static bool isChar(T &v);

    template<class T>
    static std::string getType(T &v);

};

extern _cType Type;
.

/ * sorgente.cpp * /

_cType::_cType(){};


template<class T, class U>
bool _cType::isSame(T &vl, U &vr){
    return ( typeid(vl) == typeid(vr) );
}

template<class T>
bool _cType::isInt(T &v){
    return typeid(v) == typeid(int);
}

template<class T>
bool _cType::isUInt(T &v){
    return typeid(v) == typeid(unsigned int);
}    

template<class T>
bool _cType::isFloat(T &v){
    return typeid(v) == typeid(float);
}

template<class T>
bool _cType::isLong(T &v){
    return typeid(v) == typeid(long);
}

template<class T>
bool _cType::isDouble(T &v){
    return typeid(v) == typeid(double);
}

template<class T>
bool _cType::isBool(T &v){
    return typeid(v) == typeid(bool);
}

template<class T>
bool _cType::isString(T &v){
    return typeid(v) == typeid(std::string);
}

template<class T>
bool _cType::isChar(T &v){
    return typeid(v) == typeid(char);
}

template<class T>
std::string _cType::getType(T &v){

    if ( typeid(v) == typeid(int) ) return "int";    
    else if ( typeid(v) == typeid(unsigned int) ) return "unsigned int";
    else if ( typeid(v) == typeid(float) ) return "float";
    else if ( typeid(v) == typeid(long) ) return "long";
    else if ( typeid(v) == typeid(double) ) return "double";
    else if ( typeid(v) == typeid(char) ) return "char";
    else if ( typeid(v) == typeid(std::string) ) return "std::string";
    else if ( typeid(v) == typeid(bool) ) return "bool";
    else return std::string("User defined or unknown type: ") + typeid(v).name();
}

_cType::~_cType(){};

_cType Type;
.

Per rispondere alla lettera della tua domanda (dove le altre risposte si rivolgono al suo intento), la risposta più semplice è che Visual C ++ fornisce il nome "indecorato" (non mungito) come output per std::type_info::name(), mentre nasconde il tipo di compilatore effettivo e manglatoestensione std::type_info::raw_name();La cppreference menziona anche IBM e Oracle come fornendo nomi demangled, leggibili dall'uomo, ma non ho esperienza con i loro compilatori, e quindi non posso dire se questa richiesta è accurata.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top