为什么 type_info 在命名空间 std 之外声明?
我正在使用 VS2005 和 STL 的 MS 实现。然而,类 type_info 是在“命名空间 std”之外声明的。这会给第三方库带来一些问题,除了查找 std::type_info 之外。为什么会这样,有什么解决办法吗?这是 typeinfo 开头的示例:
class type_info {
...
};
_STD_BEGIN // = namespace std {
I'm using VS2005 and the MS implementation of STL. However, the class type_info in is declared outside of "namespace std". This creates some problems for third party libs that excepts to find a std::type_info. Why is this so, and is there any workaround? Here is a sample from the beginning of typeinfo:
class type_info {
...
};
_STD_BEGIN // = namespace std {
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这很有趣 - 标准确实这么说(17.4.1.1. 库内容)
并明确指出(5.2.8 类型识别)
当然,头文件
的描述表明它应该位于命名空间std
中(18.5 类型标识):因此,
type_info
应该位于std
命名空间中(而不是在其外部)。我猜想这要么是一个错误,要么是在std
命名空间之外有一些大型代码(或小型重要代码)需要它。我本以为他们会使用一些预处理器魔法来制作它,这样您就可以根据需要强制它位于std
命名空间中(或者相反 - 使其位于std< /code> 默认情况下并允许宏或其他东西将其强制到全局命名空间)。
然而,
type_info
的另一个问题是它是typeid
运算符的结果(更准确地说,是从type_info
派生的结果),因此,可能紧密依赖于编译器对库需要符合的typeid
运算符所做的操作。因此,type_info
不在命名空间std
中的事实可能是由于编译器对typeid
表达式所做的操作,并且库编写者可能有对此几乎没有直接控制(我猜这就是为什么没有针对该问题的预处理器解决方法的原因之一)。比我更了解编译器如何工作的人必须更好地解释这一点(或者超越猜测)。但我认为你必须向 Microsoft(或 PJ Plauger/Dinkumware)的某个人询问“为什么”的真正答案。
That's interesting - the standard does say that (17.4.1.1. Library contents)
And clearly says that (5.2.8 Type identification)
Ans, of course, the descriptin of header
<typeinfo?>
indicate the it should be in namespacestd
(18.5 Type identification):So
type_info
should be in thestd
namespace (and not outside of it). 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 thestd
namespace. I'd have thought they'd use some preprocessor magic to make it so you could force it to be in thestd
namespace if desired (or the other way around - make it instd
by default and allow a macro or something to force it to the global namespace).However, one additional wrinkle for
type_info
is that it's the result of thetypeid
operator (more precisely, something derived fromtype_info
is the result), so there's probably a tight dependency on what the compiler does for thetypeid
operator that the library needs to be in line with. So the fact thattype_info
isn't in namespacestd
is possibly due to what the compiler does withtypeid
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). 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".
实际上,通过
using
声明,有一个std::type_info
。在某些情况下,它未在std
内部定义这一事实可能会成为问题,但我想知道您是否遇到过其中之一。你有什么问题吗?
With the
using
declaration, actually, there is astd::type_info
. There might be scenarios where the fact that it isn't defined inside ofstd
might be a problem, but I'd wonder if you have ran into one of them.What's your problem?
因为 Visual Studio 使用了各种技巧来允许遗留代码工作。 IIRC,标准仅规定
type_info
存在于std
命名空间中。它并不强制要求它不存在于全局命名空间中——这实际上是一个实现决策。买者自负:我还没有在标准中验证这一点。
Because Visual Studio does all sorts of tricks to allow for legacy code to work. IIRC, the Standard only states that
type_info
exist within thestd
namespace. 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.