C++中的'typeid'与'typeof'

Tim*_*Tim 144 c++ typeof typeid

我想知道有什么区别之间typeid以及typeof在C++中.这就是我所知道的:

此外,这里是我创建的测试代码测试,我发现typeid它不会返回我的预期.为什么?

main.cpp中

#include <iostream>  
#include <typeinfo>  //for 'typeid' to work  

class Person {  
    public:
    // ... Person members ...  
    virtual ~Person() {}  
};  

class Employee : public Person {  
    // ... Employee members ...  
};  

int main () {  
    Person person;  
    Employee employee;  
    Person *ptr = &employee;  
    int t = 3;  

    std::cout << typeid(t).name() << std::endl;  
    std::cout << typeid(person).name() << std::endl;   // Person (statically known at compile-time)  
    std::cout << typeid(employee).name() << std::endl; // Employee (statically known at compile-time)  
    std::cout << typeid(ptr).name() << std::endl;      // Person * (statically known at compile-time)  
    std::cout << typeid(*ptr).name() << std::endl;     // Employee (looked up dynamically at run-time  
                                                       // because it is the dereference of a pointer
                                                       // to a polymorphic class)  
 }  
Run Code Online (Sandbox Code Playgroud)

输出:

bash-3.2$ g++ -Wall main.cpp -o main  
bash-3.2$ ./main   
i  
6Person  
8Employee  
P6Person  
8Employee
Run Code Online (Sandbox Code Playgroud)

AnT*_*AnT 179

C++语言没有这样的东西typeof.您必须查看某些特定于编译器的扩展.如果您正在谈论GCC typeof,那么通过关键字在C++ 11中会出现类似的功能decltype.同样,C++没有这样的typeof关键字.

typeid是一个C++语言运算符,它在运行时返回类型标识信息.它基本上返回一个type_info与其他type_info对象相当的对象.

注意,返回type_info对象的唯一定义属性是相等和不相等,即type_info描述不同类型的对象应比较不相等,而type_info描述相同类型的对象必须比较相等.其他一切都是实现定义的.返回各种"名称"的方法不能保证返回任何人类可读的内容,甚至不能保证返回任何内容.

另请注意,上述内容可能暗示(尽管标准似乎没有明确提及它)typeid相同类型的连续应用程序可能返回不同的type_info对象(当然,它们仍然必须比较相等).

  • 仅供参考,`decltype`不是'typeof`的替代品.`typeof`也适用于类型,而`decltype`则不适用.例如,`typeof(int)`是`int`而``decltype(int)`是一个错误. (9认同)
  • *“描述不同类型的`type_info`对象比较不相等”*。实际上,这[不能保证](https://en.cppreference.com/w/cpp/types/type_info/hash_code)。不等运算符[在 C++20 中被删除](https://en.cppreference.com/w/cpp/types/type_info/operator_cmp) 是为了(我认为)不鼓励依赖不同类型来比较不相等。但如果你仔细想想,如果不平等不安全,平等也不安全。 (2认同)

Jar*_*Par 44

两者之间的主要区别如下

  • typeof是一个编译时构造,并返回编译时定义的类型
  • typeid是一个运行时构造,因此提供有关值的运行时类型的信息.

typeof参考:http://www.delorie.com/gnu/docs/gcc/gcc_36.html

typeid参考:https://en.wikipedia.org/wiki/Typeid


Bri*_*ell 26

typeid可以在运行时操作,并返回描述对象的运行时类型的对象,该对象必须是具有虚方法的类的对象的指针,以便将RTTI(运行时类型信息)存储在类中.如果没有给出具有运行时类型信息的类的指针,它还可以给出表达式或类型名称的编译时类型.

typeof是一个GNU扩展,并在编译时为您提供任何表达式的类型.例如,在声明可用于多种类型的宏中的临时变量时,这可能很有用.在C++中,您通常会使用模板.

  • 据我所知,`typeid`将接受任何表达式,而不仅仅是那些用虚方法计算对象的表达式.此外,`typeid`将接受类型*name*,而不仅仅是表达式.如果需要,可以说`typeid(5)`或`typeid(std :: string)`. (5认同)

Ara*_*raK 20

回答其他问题:

我对typeid的以下测试代码不输出正确的类型名称.怎么了?

没有任何错误.您看到的是类型名称的字符串表示形式.标准C++不会强制编译器发出类的确切名称,只需要实现者(编译器供应商)来决定什么是合适的.简而言之,名称取决于编译器.


这是两种不同的工具.typeof返回表达式的类型,但它不是标准的.在C++ 0x中有一些叫做decltypeAFAIK的工作.

decltype(0xdeedbeef) number = 0; // number is of type int!
decltype(someArray[0]) element = someArray[0];
Run Code Online (Sandbox Code Playgroud)

typeid与多态类型一起使用.例如,假设cat派生出animal:

animal* a = new cat; // animal has to have at least one virtual function
...
if( typeid(*a) == typeid(cat) )
{
    // the object is of type cat! but the pointer is base pointer.
}
Run Code Online (Sandbox Code Playgroud)


Dr.*_*ana 5

typeid 在需要时提供运行时数据的类型。Typedef 是一个编译时构造,定义了一个新类型,如下所述。C++ 输出中没有 typeof 显示为(如题注注释所示):

std::cout << typeid(t).name() << std::endl;  // i
std::cout << typeid(person).name() << std::endl;   // 6Person
std::cout << typeid(employee).name() << std::endl; // 8Employee
std::cout << typeid(ptr).name() << std::endl;      // P6Person
std::cout << typeid(*ptr).name() << std::endl;     //8Employee
Run Code Online (Sandbox Code Playgroud)