简体   繁体   English

为什么在命名空间std之外声明了type_info?

[英]Why is type_info declared outside namespace std?

I'm using VS2005 and the MS implementation of STL. 我正在使用VS2005和STL的MS实现。 However, the class type_info in is declared outside of "namespace std". 但是,类type_info in在“namespace std”之外声明。 This creates some problems for third party libs that excepts to find a std::type_info. 这为第三方库创建了一些问题,除了找到std :: type_info之外。 Why is this so, and is there any workaround? 为什么会这样,是否有任何解决方法? Here is a sample from the beginning of typeinfo: 以下是typeinfo开头的示例:

class type_info {
...
};


_STD_BEGIN // = namespace std  {

That's interesting - the standard does say that (17.4.1.1. Library contents) 这很有趣 - 标准确实说(17.4.1.1。图书馆内容)

All library entities except macros, operator new and operator delete are defined within the namespace std or namespaces nested within namespace std. 除了宏,operator new和operator delete之外的所有库实体都在命名空间std中嵌套的名称空间std或名称空间中定义。

And clearly says that (5.2.8 Type identification) 并清楚地说(5.2.8类型识别)

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. typeid表达式的结果是静态类型const std :: type_info(18.5.1)和动态类型const std :: type_info或const name的左值,其中name是从std :: type_info派生的实现定义类,它保留了行为在18.5.1中描述。

Ans, of course, the descriptin of header <typeinfo?> indicate the it should be in namespace std (18.5 Type identification): Ans,当然,header <typeinfo?>的descriptin表明它应该在namespace std (18.5 Type Identification)中:

Header <typeinfo> synopsis 标题<typeinfo>概要

 namespace std { class type_info; class bad_cast; class bad_typeid; } 

So type_info should be in the std namespace (and not outside of it). 所以type_info应该在std命名空间中(而不是在它之外)。 I guess that either this is a bug or there's some large set of code (or small set of important code) that needs it outside of the std namespace. 我想这要么是一个bug,要么是在std命名空间之外需要它的一些大型代码(或一小组重要代码)。 I'd have thought they'd use some preprocessor magic to make it so you could force it to be in the std namespace if desired (or the other way around - make it in std by default and allow a macro or something to force it to the global namespace). 我原以为他们会使用一些预处理器魔术来制作它,所以如果需要你可以强制它在std命名空间中(或者std - 默认情况下在std使用它并允许宏或某些东西强制它到全局命名空间)。

However, one additional wrinkle for type_info is that it's the result of the typeid operator (more precisely, something derived from type_info is the result), so there's probably a tight dependency on what the compiler does for the typeid operator that the library needs to be in line with. 但是, type_info另一个type_info是它是typeid运算符的结果(更确切地说,是从type_info派生的结果),因此可能严格依赖于编译器对库需要的typeid运算符所做的操作。符合。 So the fact that type_info isn't in namespace std is possibly due to what the compiler does with typeid expressions, and the library writers probably have little direct control over that (and I'd guess that's one reason why there's no preprocssor workaround for the problem). 因此, type_info不在命名空间std的事实可能是由于编译器对typeid表达式的作用,并且库编写者可能几乎没有直接控制(并且我猜这是为什么没有preprocssor解决方法的原因之一)问题)。 Someone who knows a lot more about how compilers work than I do would have to explain this better (or take it beyond speculation). 如果有更多关于编译器如何工作的人,那么就必须更好地解释这一点(或者超越推测)。

But I think you'll have to ask someone at Microsoft (or PJ Plauger/Dinkumware) for a real answer to "why". 但我认为你必须要求微软(或PJ Plauger / Dinkumware)的某个人真正回答“为什么”。

With the using declaration, actually, there is a std::type_info . 随着using的声明,其实, 还有一个std::type_info There might be scenarios where the fact that it isn't defined inside of std might be a problem, but I'd wonder if you have ran into one of them. 可能存在以下事实:未在std内部定义它可能是一个问题,但我想知道您是否遇到过其中一个问题。

What's your problem? 你怎么了?

Because Visual Studio does all sorts of tricks to allow for legacy code to work. 因为Visual Studio会执行各种技巧以允许遗留代码工作。 IIRC, the Standard only states that type_info exist within the std namespace. IIRC,标准仅声明type_info存在于std命名空间内。 It does not mandate that it not exist within the global namespace - that is really an implementation decision. 它并不强制它不存在于全局命名空间内 - 这实际上是一个实现决策。

Caveat Emptor: I haven't verified this in the Standard. 警告:我没有在标准中验证这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM