typeid 运算符返回的对象的生命周期是多少?

发布于 2024-11-28 19:56:31 字数 215 浏览 2 评论 0原文

如果我调用 typeid 并检索返回的 type_info 的地址:

const type_info* info = &( typeid( Something ) );

typeid 返回的对象的生命周期是多少以及指向该对象的指针将持续多长时间对象仍然有效吗?

If I call typeid and retrieve the address of returned type_info:

const type_info* info = &( typeid( Something ) );

what's the lifetime of the object returned by typeid and how long will the pointer to that object remain valid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从﹋此江山别 2024-12-05 19:56:31

无论实现如何实现它们,typeid 表达式的结果都是左值,并且这些左值引用的对象的生命周期必须持续到程序结束。

来自 ISO/IEC 14882:2003 5.2.8 [expr.typeid]:

typeid 表达式的结果是一个左值 [...]左值引用的对象的生命周期延伸到程序结束。

However the implementation implements them, the results of typeid expressions are lvalues and the lifetime of the objects that those lvalues refer to must last until the end of the program.

From ISO/IEC 14882:2003 5.2.8 [expr.typeid]:

The result of a typeid expression is an lvalue [...] The lifetime of the object referred to by the lvalue extends to the end of the program.

深海不蓝 2024-12-05 19:56:31

从C++ 2003标准的5.2.8.1开始:

typeid 表达式的结果是静态类型 const 的左值
std::type_info (18.5.1) 和动态类型 const std::type_info 或 const
name 其中 name 是派生自的实现定义的类
std::type_info 保留 18.5.1.61 中描述的行为)
左值引用的对象的生命周期延伸到
程序结束
。是否调用析构函数
程序末尾的 type_info 对象未指定。

From 5.2.8.1 of C++ 2003 standard:

The result of a typeid expression is an lvalue of static type const
std::type_info (18.5.1) and dynamic type const std::type_info or const
name where name is an implementation-defined class derived from
std::type_info which preserves the behavior described in 18.5.1.61)
The lifetime of the object referred to by the lvalue extends to the
end of the program
. Whether or not the destructor is called for the
type_info object at the end of the program is unspecified.

流星番茄 2024-12-05 19:56:31

它的生命周期就是程序的持续时间。无论您编写多少次 typeid(x),它每次都会返回相同的 type_info 对象,且类型相同。

也就是说,

 T x, y;
 const type_info & xinfo = typeid(x);
 const type_info & yinfo = typeid(y);

引用xinfoyinfo 都引用同一个对象。因此,尝试打印地址来验证它:

 cout << &xinfo << endl; //printing the address
 cout << &yinfo << endl; //printing the address

输出:

0x80489c0
0x80489c0

注意:对于您的运行,地址可能与上面的不同,但无论它是什么,它都会是相同的!

演示:http://www.ideone.com/jO4CO

Its lifetime is the duration of the program. And no matter how many times you write typeid(x), it will return the same type_info object everytime, for same type.

That is,

 T x, y;
 const type_info & xinfo = typeid(x);
 const type_info & yinfo = typeid(y);

The references xinfo and yinfo both refer to the same object. So try printing the address to verify it:

 cout << &xinfo << endl; //printing the address
 cout << &yinfo << endl; //printing the address

Output:

0x80489c0
0x80489c0

Note: for your run, the address might be different from the above, but whatever it is, it will be same!

Demo : http://www.ideone.com/jO4CO

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文