Use the TypeID operator to create a C++ data type

Operator typeThe type used to define the data must include header files . this isoperator or workernot a jobRelated size It is also operational code, not a function.

The .name() member function returns a C-style String type, which is the “name” of the data type, or understood as a form of “shorthand”. The specific values ​​are determined by the implementation of the compiler, and the compiler may be different (the criterion that each type returns a unique string must be applied).

Application:
TypeId(variable, constant, or datatype).name().

The following writing methods are allowed:
int exponent=123;
cout < typeid(exponent).Noun();
cout < typeid(int).Noun();
cout < typeid(123).Noun();

Except: the autofill type cannot be used directly: typeid (default)

The following code is where I set up most of the data to check for their respective return types:

note. The source code uses an auto variable, please bring the compile command parameter when compiling -std C++ 1y

#include <iostream>
#include <typeinfo>
#include <string>
#include <array>
#include <vector>
#include <list>
#include <set>
#include <map>
 
#define idType(s) cout<<typeid(s).name()<<endl
 
using namespace std;
 
string myStr(string s){
	return s;
}
 
class cc
{
public:
    virtual void vvf() {}
};
 
class cpp : public cc {};
 
int main(void)
{
	string a = "abcd";
	string b = "1234567890";
	// ""
	// string("")
	const char *c = "abc123";
	char d[30] = "abc";
	char e="a";
	short f = 123;
	int g = 123456789;
	long h = 123456789;
	long long i = 1234567890;
	float j = 123.456;
	double k = 1.23e-45;
	long double l = 1.2e+308;
	bool m = true;
	void *n;
	char *o;
	int *p;
	long *q;
	string *r;
	struct ss {long l;};
	struct tt {int i=0;int j=1;};
	union uu {char name;int id;};
	enum vv { Mon=0, Tue = 1, Thi };
	class ww {};
	auto x = "abc";
	auto y = 1;
	auto z = -1.23;
	
	unsigned int ui;
	unsigned char uc;
	unsigned short us;
	unsigned long ul;
	unsigned long long ull;

	size_t pos;
	array<int,6> arr;
	vector<string> vct;
	list<int> lst;
	set<int> set;
	map<int,string> map;
	// существует около 16 типов контейнера, другие слегка 
				
	cout << typeid(a).name() << endl;
	cout << typeid(b).name() << endl;
	cout << typeid("").name() <<endl;
	cout << typeid(string("")).name() <<endl;
	idType(c);
	idType(d);
	idType(e);
	idType(f);
	idType(g);
	idType(h);
	idType(i);
	idType(j);
	idType(k);
	idType(l);
	idType(m);
	idType(n);
	idType(o);
	idType(p);
	idType(q);
	idType(r);
	idType(ss);
	idType(tt);
	idType(uu);
	idType(vv);
	idType(ww);
	idType(x);
	idType(y);
	idType(z);
	
	cout<<endl;
	idType(ui);
	idType(uc);
	idType(us);
	idType(ul);
	idType(ull);

	cout<<endl;
	idType(pos);
	idType(arr);
	idType(lst);
	idType(vct);
	idType(set);
	idType(map);
	
	cout<<endl;
	idType(&a);
	idType(&f);
	idType(&m);
	idType(&x);
	idType(&y);
	idType(&z);
	
	cout<<endl;
	cpp* p1 = new cpp;
    cc* p2 = p1;
    idType(p1);
    idType(p2);
    idType(*p1);
    idType(*p2);
    idType(&p1);
    idType(&p2);
	
	cout<<endl;
	 Idype (typeId (a) .name ()); // typeId (). name () Тип возвращаемого значения Тип 
	 Idype (main ()); // main () функция 
	 Idype (MyStr (""); // xxxx () Функция 
	 Idype (main); // main () Тип функции 
	 Idype (mystr); // xxxx () Функция функции 
		
	return 0;
}

Test results:

SS
SS
A1_c
SS
PKC
A30_c
c
s
me
to
x
F
Dr
e
B
bff
PC
with me
R
PSs
Z4mainE2ss
Z4mainE2tt
Z4mainE2uu
Z4mainE2vv
Z4main
PKC
me
Dr

j
h
R
M
y

y
St5ArrayIiLy6EE
St4listIiSaIiEE
St6vectorISsSeeSEE
St3setIiSt4lessIiESaIiEE
St3mapIiSsSt4lessIiESaISt4pairIKiSsEEE

PSs
note
Lead
PPKc
with me
P.D

p3cpp
P2cc
3cpp
3cpp
PP3cpp
PP2cc

PKC
me
SS
five
FSsSsE

——————————–
The process terminated after 1.043 seconds with a return value of 0
Please press any key.

From the results, you can summarize the figure below: (a hard hand to make me a watermark ^_^)

933fce19f1cffbfb0bc98cbbea113de4

Most of the return results can be understood, and some values ​​describe the length; However, it is difficult to get container type description rules for a response other than the contents of the name.

TypeID application

Since the return value of the Web TypeID is determined by the respective compilers, there is no point in remembering the names of these “shortcut” data. Fortunately, TypeId reloaded == and ! = judge in type_info class. Be careful not to use if (typeid (x).name == “ss”), but you can use if (typeid (x).Name. == typeId (int).name), if (typeId (x).name == typeId(1.0f).Name) to specify the data type of x:

void exam(auto x)
{

if (typeid(x).name()==typeid("").name()) {
		

	// do something ...

	}

}

Application

Source code structure

layers
type_info // Contains some type information generated by the application. This is the class returned by the typeid operator. (Class)
bad_typeid // Exception thrown if an argument in a typeid expression is null (class)
bad_cast // The exception thrown by the dynamic_cast expression is invalid, that is, the reference type (class) failed

summary
standard namespace {
type_info class;
bad_cast class;
bad_typeid class;
}

STD category:: type_info
class type_info{
general:
virtual ~type_info();
Logical operator or worker==(const type_info & rhs) no exception;
Logical operator or worker! =(const type_info & rhs) no exception;
Logical before (const type_info & rhs) There is no exception;
size_t hash_code() There is no exception;
const letter * Noun() There is no exception;
type_info(const type_info & rhs) = delete; // cannot be copied
type_info & operator = (const type_info & rhs) = delete; // cannot be copied
};

STD Category:: bad_cast
bad_cast class: global exception {
general:
bad_cast() noexcept;
bad_cast (const bad_cast &) no exception;
bad_cast & operator = (const bad_cast &) noexcept;
const char * what() const noexcept override;
};

STD category:: bad_typeid
bad_typeid class: global exception {
general:
bad_typeid() noexcept;
bad_typeid (const bad_typeid &) noexcept;
bad_typeid & operator = (const bad_typeid &) noexcept;
const char * what() const noexcept override;
};

Leave a Comment