English 中文(简体)
Why is type_info declared outside 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  {
最佳回答

That s interesting - the standard does say that (17.4.1.1. Library contents)

All library entities except macros, operator new and operator delete are defined within the namespace std or namespaces nested within namespace std.

And clearly says that (5.2.8 Type identification)

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.

Ans, of course, the descriptin of header <typeinfo?> indicate the it should be in namespace std (18.5 Type identification):

Header <typeinfo> synopsis

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). 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. 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).

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. 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). 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".

问题回答

With the using declaration, actually, there is a 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.

What s your problem?

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 the std 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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签