C++ 中的“typeid”与“typeof”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1986418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'typeid' versus 'typeof' in C++
提问by Tim
I am wondering what the difference is between typeid
and typeof
in C++. Here's what I know:
我想知道C++typeid
和typeof
C++之间有什么区别。这是我所知道的:
typeid
is mentioned in the documentation for type_infowhich is defined in the C++ header file typeinfo.typeof
is defined in the GCC extension for C and in the C++ Boostlibrary.
typeid
在 C++ 头文件typeinfo 中定义的type_info的文档中提到了 。typeof
在 C 的 GCC 扩展和 C++ Boost库中定义。
Also, here is test code test that I've created where I've discovered that typeid
does not return what I expected. Why?
此外,这里是我创建的测试代码测试,我发现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)
}
output:
输出:
bash-3.2$ g++ -Wall main.cpp -o main
bash-3.2$ ./main
i
6Person
8Employee
P6Person
8Employee
回答by AnT
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 keyword decltype
. Again, C++ has no such typeof
keyword.
C++ 语言没有typeof
. 您必须查看一些特定于编译器的扩展。如果您在谈论 GCC 的typeof
,那么 C++11 中通过关键字 存在类似的功能decltype
。同样,C++ 没有这样的typeof
关键字。
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.
typeid
是一个 C++ 语言运算符,它在运行时返回类型标识信息。它基本上返回一个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.
请注意,返回type_info
对象的唯一定义属性是它具有相等性和非相等性可比性,即type_info
描述不同类型的对象将比较不相等,而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
同一类型的连续应用程序可能会返回不同的type_info
对象(当然,它们仍然必须比较相等)。
回答by JaredPar
The primary difference between the two is the following
两者的主要区别如下
- typeof is a compile time construct and returns the type as defined at compile time
- typeid is a runtime construct and hence gives information about the runtime type of the value.
- typeof 是一个编译时构造并返回在编译时定义的类型
- typeid 是一个运行时构造,因此提供有关值的运行时类型的信息。
typeof Reference: http://www.delorie.com/gnu/docs/gcc/gcc_36.html
类型参考:http: //www.delorie.com/gnu/docs/gcc/gcc_36.html
typeid Reference: https://en.wikipedia.org/wiki/Typeid
typeid 参考:https: //en.wikipedia.org/wiki/Typeid
回答by Brian Campbell
typeid
can operate at runtime, and return an object describing the run time type of the object, which must be a pointer to an object of a class with virtual methods in order for RTTI (run-time type information)to be stored in the class. It can also give the compile time type of an expression or a type name, if not given a pointer to a class with run-time type information.
typeid
可以在运行时进行操作,并返回一个描述对象运行时类型的对象,该对象必须是指向具有虚方法的类的对象的指针,以便RTTI(运行时类型信息)存储在类中。如果没有给出指向具有运行时类型信息的类的指针,它还可以给出表达式的编译时类型或类型名称。
typeof
is a GNU extension, and gives you the type of any expression at compile time. This can be useful, for instance, in declaring temporary variables in macros that may be used on multiple types. In C++, you would usually use templatesinstead.
typeof
是 GNU 扩展,并在编译时为您提供任何表达式的类型。例如,这在声明可用于多种类型的宏中的临时变量时很有用。在 C++ 中,您通常会改用模板。
回答by AraK
Answering the additional question:
回答附加问题:
my following test code for typeid does not output the correct type name. what's wrong?
我下面的 typeid 测试代码没有输出正确的类型名称。怎么了?
There isn't anything wrong. What you see is the string representation of the type name. The standard C++ doesn't force compilers to emit the exact name of the class, it is just up to the implementer(compiler vendor) to decide what is suitable. In short, the names are up to the compiler.
没有什么不对的。您看到的是类型名称的字符串表示形式。标准 C++ 不强制编译器发出类的确切名称,它只是由实现者(编译器供应商)决定什么是合适的。简而言之,名称由编译器决定。
These are two different tools. typeof
returns the type of an expression, but it is not standard. In C++0x there is something called decltype
which does the same job AFAIK.
这是两种不同的工具。typeof
返回表达式的类型,但它不是标准的。在 C++0x 中,有一种叫做decltype
AFAIK 的东西。
decltype(0xdeedbeef) number = 0; // number is of type int!
decltype(someArray[0]) element = someArray[0];
Whereas typeid
is used with polymorphic types. For example, lets say that cat
derives animal
:
而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.
}
回答by Dr. Debasish Jana
typeid provides the type of the data at runtime, when asked for. Typedef is a compile time construct that defines a new type as stated after that. There is no typeof in C++ Output appears as (shown as inscribed comments):
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
回答by Patrick K
You can use Boost demangle to accomplish a nice looking name:
你可以使用 Boost demangle 来完成一个漂亮的名字:
#include <boost/units/detail/utility.hpp>
and something like
和类似的东西
To_main_msg_evt ev("Failed to initialize cards in " + boost::units::detail::demangle(typeid(*_IO_card.get()).name()) + ".\n", true, this);