获取C++中数据的类型的方法
本文最后更新于:2022年3月19日 凌晨
此文来自于👉 C++ / typeinfo — DevDocs ,并整理
判断数据是哪种数据类型#
头文件<typeinfo>
std::type_info::name()#
例子:
#include <iostream>
#include <typeinfo>
struct Base { virtual ~Base() = default; };
struct Derived : Base {};
int main() {
Base b1;
Derived d1;
const Base *pb = &b1;
std::cout << typeid(*pb).name() << '\n';
pb = &d1;
std::cout << typeid(*pb).name() << '\n';
int i;
int * pi;
std::cout << "int is: " << typeid(int).name() << '\n';
std::cout << " i is: " << typeid(i).name() << '\n';
std::cout << " pi is: " << typeid(pi).name() << '\n';
std::cout << "*pi is: " << typeid(*pi).name() << '\n';
return 0;
}
结果:
4Base
7Derived
int is: int
i is: int
pi is: int *
*pi is: int
判断数据是否是对应的数据类型#
头文件<type_traits>
主要类型类别 Primary type categories#
-
Is array (class template )
// is_array example #include <iostream> #include <array> #include <string> #include <type_traits> int main() { std::cout << std::boolalpha; std::cout << "is_array:" << std::endl; std::cout << "int: " << std::is_array<int>::value << std::endl; std::cout << "int[3]: " << std::is_array<int[3]>::value << std::endl; std::cout << "array<int,3>: " << std::is_array<std::array<int,3>>::value << std::endl; std::cout << "string: " << std::is_array<std::string>::value << std::endl; std::cout << "string[3]: " << std::is_array<std::string[3]>::value << std::endl; return 0; }
结果:
is_array: int: false int[3]: true array<int,3>: false string: false string[3]: true
-
Is non-union class (class template )
-
Is enum (class template )
-
Is floating point (class template )
-
Is function (class template )
-
Is integral (class template )
-
Is lvalue reference (class template )
-
Is member function pointer (class template )
-
Is member object pointer (class template )
-
Is pointer (class template )
-
Is rvalue reference (class template )
-
Is union (class template )
-
Is void (class template )
复合类型范畴 Composite type categories#
-
Is arithmetic type (class template )
// is_arithmetic example #include <iostream> #include <type_traits> #include <complex> int main() { std::cout << std::boolalpha; std::cout << "is_arithmetic:" << std::endl; std::cout << "char: " << std::is_arithmetic<char>::value << std::endl; std::cout << "float: " << std::is_arithmetic<float>::value << std::endl; std::cout << "float*: " << std::is_arithmetic<float*>::value << std::endl; std::cout << "complex<double>: " << std::is_arithmetic<std::complex<double>>::value << std::endl; return 0; }
Output:
is_arithmetic:
char: true
float: true
float*: false
complex<double>: false
-
Is compound type (class template )
-
Is fundamental type (class template )
-
Is member pointer type (class template )
-
Is object type (class template )
-
Is reference type (class template )
-
Is scalar type (class template )
类型属性 Type properties#
-
Is abstract class (class template )
// is_abstract example #include <iostream> #include <type_traits> struct A { }; struct B { virtual void fn() = 0; // pure virtual function }; struct C : B { }; struct D : C { virtual void fn(){} }; int main() { std::cout << std::boolalpha; std::cout << "is_abstract:" << std::endl; std::cout << "A: " << std::is_abstract<A>::value << std::endl; std::cout << "B: " << std::is_abstract<B>::value << std::endl; std::cout << "C: " << std::is_abstract<C>::value << std::endl; std::cout << "D: " << std::is_abstract<D>::value << std::endl; return 0; }
结果:
is_abstract:
A: false
B: true
C: true
D: false
-
Is const-qualified (class template )
-
Is empty class (class template )
-
Is literal type (class template )
-
Is POD type (class template )
-
Is polymorphic (class template )
-
Is signed type (class template )
-
Is standard-layout type (class template )
-
Is trivial type (class template )
-
Is trivially copyable (class template )
-
Is unsigned type (class template )
-
Is volatile-qualified (class template )
类型特征 Type features#
-
Has virtual destructor (class template )
// has_virtual_destructor example #include <iostream> #include <type_traits> struct A { }; struct B { virtual ~B(){} }; struct C : B { }; int main() { std::cout << std::boolalpha; std::cout << "has_virtual_destructor:" << std::endl; std::cout << "int: " << std::has_virtual_destructor<int>::value << std::endl; std::cout << "A: " << std::has_virtual_destructor<A>::value << std::endl; std::cout << "B: " << std::has_virtual_destructor<B>::value << std::endl; std::cout << "C: " << std::has_virtual_destructor<C>::value << std::endl; return 0; }
Output:
has_virtual_destructor:
int: false
A: false
B: true
C: true
-
Is assignable (class template )
-
Is constructible (class template )
-
Is copy assignable (class template )
-
Is copy constructible (class template )
-
Is destructible (class template )
-
Is default constructible (class template )
-
Is move assignable (class template )
-
Is move constructible (class template )
-
Is trivially assignable (class template )
-
Is trivially constructible (class template )
-
Is trivially copy assignable (class template )
is_trivially_copy_constructible
Is trivially copy constructible (class template )
-
Is trivially destructible (class template )
is_trivially_default_constructible
Is trivially default constructible (class template )
-
Is trivially move assignable (class template )
is_trivially_move_constructible
Is trivially move constructible (class template )
-
Is assignable throwing no exceptions (class template )
-
Is constructible throwing no exceptions (class template )
-
Is copy assignable throwing no exceptions (class template )
-
Is copy constructible throwing no exceptions (class template )
-
Is nothrow destructible (class template )
is_nothrow_default_constructible
Is default constructible throwing no exceptions (class template )
-
Is move assignable throwing no exception (class template )
-
Is move constructible throwing no exceptions (class template )
类型关系 Type relationships#
-
Is base class of (class template )
// is_base_of example #include <iostream> #include <type_traits> struct A {}; struct B : A {}; int main() { std::cout << std::boolalpha; std::cout << "is_base_of:" << std::endl; std::cout << "int, int: " << std::is_base_of<int,int>::value << std::endl; std::cout << "A, A: " << std::is_base_of<A,A>::value << std::endl; std::cout << "A, B: " << std::is_base_of<A,B>::value << std::endl; std::cout << "A, const B: " << std::is_base_of<A,const B>::value << std::endl; std::cout << "A&, B&: " << std::is_base_of<A&,B&>::value << std::endl; std::cout << "B, A: " << std::is_base_of<B,A>::value << std::endl; return 0; }
Output:
is_base_of:
int, int: false
A, A: true
A, B: true
A, const B: true
A&, B&: false
B, A: false
-
Is convertible (class template )
-
Is same type (class template )
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!