gpt4 book ai didi

c++ - 关于 typeinfo 中的 before 函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:35:30 29 4
gpt4 key购买 nike

我正在阅读现代 C++ 设计。以下是摘自类型信息描述的文本片段。

The before member function introduces an ordering relationship for type_info objects. Using type_info::before, you can perform indexing on type_info objects.

If you want to sort type_info objects, again you must actually store pointers to type_info, and this time you must use the before member function. Consequently, if you want to use STL's ordered containers with type_info, you must write a little functor and deal with pointers.

在上面的描述中,对 type_info 对象进行索引是什么意思,我们如何使用“before”函数来实现这一点。

另一个问题是作者的意思是如果我们想使用带有 type_info 的 STL 容器,我们必须编写一个小仿函数并处理指针。

谢谢!

最佳答案

问题是您不知道是否每次都获得相同的 type_info 对象,或者是否有多个对象引用相同的类型。因此,您不能只比较 type_info 对象本身。

before() 函数必须以特定于实现的方式对此进行排序,并返回正确的顺序。

关于c++ - 关于 typeinfo 中的 before 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5205277/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com