views:

349

answers:

3

Hi, 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  {
+1  A: 

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?

sbi
Sorry, I copied from the wrong <typeinfo> class. The one I was using was actually for Windows CE (also by MS), and it lacks the using directive. Is this a bug in that specific version of STL?
Rolle
If the standard defines a class as belonging in `std`, then it should be defined as such or ADL (Koenig lookup) may break.
MSalters
A: 

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.

D.Shawley
Actually, the reason why the standard reserves the underscore prefix for the implementation is because implementors can't just claim ordinary names. `::_typeinfo` would be allowed.
MSalters
+3  A: 

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

Michael Burr
OK I found a comment in the typeinfo file. Parafracing, it said "type_info not included in std namespace as RTTI compilers often have an own imlpementation". I solved it by simply moving it into std, against the recommendation in the comment.
Rolle
Could you give a little more info on that comment's location and/or the compiler version for the `<typeinfo>` header you saw it in?
Michael Burr
Just in the beginning of the file, under all includes and a pragma call. The file is for wince 6, and probably came either with Platform Builder or with a pre-built SDK sometime long ago. It is copyright Microsoft. Here is the full comment:// Return type from typeid operator, according to the standard// this should really be in namespace std, but the compiler// has built-in knowledge about this class. Do not modify// anything here without checking the layout of RTTI information// generated by the compiler.
Rolle