'typeid'与'typeof'在C ++中 [英] 'typeid' versus 'typeof' in C++

查看:164
本文介绍了'typeid'与'typeof'在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道C ++中的 typeid typeof 之间的区别是什么。这是我知道的:

I am wondering what the difference is between typeid and typeof in C++. Here's what I know:

  • typeid is mentioned in the documentation for type_info which is defined in the C++ header file typeinfo.

typeof 在C的GCC扩展中和C ++ Boost 库中定义。

typeof is defined in the GCC extension for C and in the C++ Boost library.

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

Also, here is test code test that I've created where I've discovered that typeid does not return what I expected. Why?

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)  
 }  

输出:

bash-3.2$ g++ -Wall main.cpp -o main  
bash-3.2$ ./main   
i  
6Person  
8Employee  
P6Person  
8Employee


推荐答案

没有这样的事情 typeof 。您必须查看一些特定于编译器的扩展。如果你正在谈论GCC的 typeof ,那么C ++ 11中通过关键字 decltype auto 。再次,C ++没有这样的 typeof 关键字。

C++ language has no such thing as typeof. You must be looking at some compiler-specific extension. If you are talking about GCC's typeof, then a similar feature is present in C++11 through the keywords decltype and auto. Again, C++ has no such typeof keyword.

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

typeid is a C++ language operator which returns type identification information at run time. It basically returns a type_info object, which is equality-comparable with other type_info objects.

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

Note, that the only defined property of the returned type_info object has is its being equality- and non-equality-comparable, i.e. type_info objects describing different types shall compare non-equal, while type_info objects describing the same type have to compare equal. Everything else is implementation-defined. Methods that return various "names" are not guaranteed to return anything human-readable, and even not guaranteed to return anything at all.

注意,上面可能暗示(虽然该标准似乎没有明确提及) typeid 到相同类型的连续应用程序可能返回不同的 type_info 对象(当然,仍然必须比较相等)。

Note also, that the above probably implies (although the standard doesn't seem to mention it explicitly) that consecutive applications of typeid to the same type might return different type_info objects (which, of course, still have to compare equal).

这篇关于'typeid'与'typeof'在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆