When can typeid return different type_info instances for same type?(typeid 什么时候可以为同一类型返回不同的 type_info 实例?)
问题描述
Andrei Alexandrescu 在 现代C++设计:
typeid
返回的对象有静态存储,所以你不必担心终身问题.
The objects returned by
typeid
have static storage, so you don't have to worry about lifetime issues.
安德烈继续:
标准不保证每次调用,比如说,typeid(int)
返回相同的引用type_info
对象.
The standard does not guarantee that each invocation of, say,
typeid(int)
returns a reference to the sametype_info
object.
虽然标准不保证这一点,但在 GCC 和 Visual Studio 等常见编译器中是如何实现的?
Even though the standard does not guarantee this, how is this implemented in common compilers, such as GCC and Visual Studio?
假设 typeid
没有泄漏(并且每次调用都返回一个新实例),它是每个应用程序、每个翻译单元、每个 dll/so 一个表",还是完全不同的东西?
Assuming typeid
does not leak (and return a new instance every call), is it one "table" per application, per translation unit, per dll/so, or something completely different?
是否有&typeid(T) != &typeid(T)
的时候?
我主要对 Windows 编译器感兴趣,但也感谢任何有关 Linux 和其他平台的信息.
I'm mainly interested in compilers for Windows, but any information for Linux and other platforms is also appreciated.
推荐答案
是否有 &typeid(T) != &typeid(T) 的时候?
Are there times when &typeid(T) != &typeid(T)?
我主要对 Windows 编译器感兴趣,但也感谢任何有关 Linux 和其他平台的信息.
I'm mainly interested in compilers for Windows, but any information for Linux and other platforms is also appreciated.
是的.因此,在 windows 下 DLL 不能有未解析的符号.如果你有:
Yes. Under windows DLL can't have unresolved symbols, thus. If you have:
foo.h
struct foo { virtual ~foo() {} };
dll.cpp
#include "foo.h"
...
foo f;
cout << &typeid(&f) << endl
main.cpp
#include "foo.h"
...
foo f;
cout << &typeid(&f) << endl
会给你不同的指针.因为在加载 dll 之前 typeid(foo) 应该存在在 dll 和主 exe 中
Would give you different pointers. Because before dll was loaded typeid(foo) should exist in both dll and primary exe
此外,在 Linux 下,如果未使用 -rdynamic(或 --export-dynamic)编译主可执行文件,则 typeid 将解析为可执行文件中的不同符号,并且在共享对象中(在 ELF 平台下通常不会发生),因为在链接可执行文件时进行了一些优化——删除了不必要的符号.
More then that, under Linux, if main executable was not compiled with -rdynamic (or --export-dynamic) then typeid would be resolved to different symbols in executable and in shared object (which usually does not happen under ELF platforms) because of some optimizations done when linking executable -- removal of unnecessary symbols.
这篇关于typeid 什么时候可以为同一类型返回不同的 type_info 实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:typeid 什么时候可以为同一类型返回不同的 type_info 实例?
基础教程推荐
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01