首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

typeid operator

查询类型的信息。

用于动态类型一种多态对象必须已知并用于静态类型识别。

句法

typeid( type )

(1)

?

typeid( expression )

(2)

?

标头<typeinfo>必须包括在使用typeid%28如果不包括标题,则每次使用关键字typeid使程序格式不正确.%29.

类型表达式是lvalue表达式的对象。静态存储持续时间,多态类型conststd::type_info或者从它衍生出来的某种类型的。

解释

1%29指std::type_info表示类型类型的。如果类型是引用类型,则结果引用std::type_info表示引用类型的。

2%29检查表达式

如果表达式为极值表达标识对象的多态类型。%28,即声明或继承至少一个虚函数%29,typeid表达式计算表达式,然后引用std::type_info对象,该对象表示表达式的动态类型。如果通过应用一元获得glvalue表达式,则为%2A运算符指向指针,指针是空指针值,是类型的例外。std::bad_typeid或派生自std::bad_typeid被扔了。

B%29如果表达式不是多态类型的glvalue表达式,typeid是吗?不计算表达式,以及std::type_info对象表示表达式的静态类型。不执行Lvalue到rvalue、数组到指针或函数到指针的转换.临时物化但是,对于prvalue参数执行的是%28形式的%29:Typeid决定结果对象的类型。%28自C++17%29

在所有情况下,cv-限定符都被类型%28忽略,即,typeid(T) == typeid(const T)29%。

如果操作数为typeid是类类型或对类类型的引用,则该类类型不能是不完全类型...

如果typeid在析构函数或构造函数中用于正在构造或销毁的对象(包括构造函数%27)中。初始化程序列表或默认成员初始化器%29,则std::type_info对象所引用的typeid表示正在构造或销毁的类,即使它不是派生最多的类。

关键词

typeid...

注记

当应用于多态类型的表达式时,类型表达式的计算可能涉及运行时开销%28a虚拟表查找%29,否则类型表达式在编译时解析。

对象的析构函数是否为typeid在程序结束时执行。

也不能保证std::type_info实例将被同一类型上的type id表达式的所有计算所引用,尽管std::type_info::hash_code这类[医]info对象将是相同的,就像它们的std::type_index...

二次

代码语言:javascript
复制
const std::type_info& ti1 = typeid(A);
const std::type_info& ti2 = typeid(A);
 
assert(&ti1 == &ti2); // not guaranteed
assert(ti1.hash_code() == ti2.hash_code()); // guaranteed
assert(std::type_index(ti1) == std::type_index(ti2)); // guaranteed

二次

使用其中一个实现显示输出的示例,其中类型[医]名称打印完整类型的名称;如果使用gcc或类似的名称,则通过c++FILT-t筛选。

二次

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <typeinfo>
 
struct Base {}; // non-polymorphic
struct Derived : Base {};
 
struct Base2 { virtual void foo() {} }; // polymorphic
struct Derived2 : Base2 {};
 
int main() {
    int myint = 50;
    std::string mystr = "string";
    double *mydoubleptr = nullptr;
 
    std::cout << "myint has type: " << typeid(myint).name() << '\n'
              << "mystr has type: " << typeid(mystr).name() << '\n'
              << "mydoubleptr has type: " << typeid(mydoubleptr).name() << '\n';
 
    // std::cout << myint is a glvalue expression of polymorphic type; it is evaluated
    const std::type_info& r1 = typeid(std::cout << myint);
    std::cout << "std::cout<<myint has type : " << r1.name() << '\n';
 
    // std::printf() is not a glvalue expression of polymorphic type; NOT evaluated
    const std::type_info& r2 = typeid(std::printf("%d\n", myint));
    std::cout << "printf(\"%d\\n\",myint) has type : " << r2.name() << '\n';
 
    // Non-polymorphic lvalue is a static type
    Derived d1;
    Base& b1 = d1;
    std::cout << "reference to non-polymorphic base: " << typeid(b1).name() << '\n';
 
    Derived2 d2;
    Base2& b2 = d2;
    std::cout << "reference to polymorphic base: " << typeid(b2).name() << '\n';
 
    try {
        // dereferencing a null pointer: okay for a non-polymoprhic expression
        std::cout << "mydoubleptr points to " << typeid(*mydoubleptr).name() << '\n'; 
        // dereferencing a null pointer: not okay for a polymorphic lvalue
        Derived2* bad_ptr = nullptr;
        std::cout << "bad_ptr points to... ";
        std::cout << typeid(*bad_ptr).name() << '\n';
    } catch (const std::bad_typeid& e) {
         std::cout << " caught " << e.what() << '\n';
    }
}

二次

可能的产出:

二次

代码语言:javascript
复制
myint has type: int
mystr has type: std::basic_string<char, std::char_traits<char>, std::allocator<char> >
mydoubleptr has type: double*
50std::cout<<myint has type : std::basic_ostream<char, std::char_traits<char> >
printf("%d\n",myint) has type : int
reference to non-polymorphic base: Base
reference to polymorphic base: Derived2
mydoubleptr points to double
bad_ptr points to...  caught std::bad_typeid

二次

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com