typeid operator

M

Marco Jez

Hi everyone!

I would like to use the reference returned by typeid as key in a std::map.
Is it safe to assume that typeid(T) (where T is a type name) will always
return the same reference to the same type_info structure for a given T? My
map would look like this:

typedef std::map<const std::type_info &, ......> Type_map;

Cheers,
Marco
 
A

Andrew Koenig

I would like to use the reference returned by typeid as key in a std::map.
Is it safe to assume that typeid(T) (where T is a type name) will always
return the same reference to the same type_info structure for a given T?

I don't see why you should be able to make that assumption.

The type_info::before member is intended to be used for defining a
comparison function that you can use for making map keys out of typeids. If
you use it, two distinct type_info objects that represent the same type
should be treated as equivalent keys.
 
V

Victor Bazarov

Marco said:
I would like to use the reference returned by typeid as key in a std::map.
Is it safe to assume that typeid(T) (where T is a type name) will always
return the same reference to the same type_info structure for a given T? My
map would look like this:

typedef std::map<const std::type_info &, ......> Type_map;

Yes, that's guaranteed.

V
 
V

Victor Bazarov

Andrew said:
I don't see why you should be able to make that assumption.

I think the Standard in 5.2.8 says that the lvalue is returned and that
the lifetime of the object referred to by the lvalue is entire program.
So, why should we be able to make that assumption? Or did you forget the
'not' as in "I don't see why you should _not_ be able..."?

The only doubt I have is that the OP's map is made to have the reference
as the key type. Is it possible? "Key" is required to be assignable.
Are references assignable? I kind of think they are fine, but are they?
The type_info::before member is intended to be used for defining a
comparison function that you can use for making map keys out of typeids. If
you use it, two distinct type_info objects that represent the same type
should be treated as equivalent keys.

V
 
J

Jonathan Turkanis

Victor Bazarov said:
I think the Standard in 5.2.8 says that the lvalue is returned and that
the lifetime of the object referred to by the lvalue is entire program.
So, why should we be able to make that assumption? Or did you forget the
'not' as in "I don't see why you should _not_ be able..."?

Wouldn't it be legal -- but insane -- for an implementation to have several
type_info objects for a given type, each of which has lifetime equal to the
entire program, and for typeid to to select one at random each time typeid is
invoked?

Perhaps support for dynamic libraries might sometimes lead to the existence of
two typeid objects for the same type.

Jonathan
 
M

Marco Jez

I don't see why you should be able to make that assumption.

That's why I've asked... :)
Since it seemed to be true in my implementation I was wondering whether the
Standard guarantees that or not. Anyway I didn't notice the before() member,
my fault.
The type_info::before member is intended to be used for defining a
comparison function that you can use for making map keys out of typeids.
If you use it, two distinct type_info objects that represent the same type
should be treated as equivalent keys.

So, would this map be guaranteed to work as expected?

struct Type_info_cmp
{
bool operator()(const std::type_info *t1, const std::type_info *t2)
const
{
return t1->before(*t2) != 0;
}
};

typedef std::map<const std::type_info*, My_struct, Type_info_cmp>
Type_map;

Cheers,
Marco
 
M

Marco Jez

The only doubt I have is that the OP's map is made to have the reference
as the key type. Is it possible? "Key" is required to be assignable.
Are references assignable? I kind of think they are fine, but are they?

I was actually thinking of storing a pointer to the type_info structure, not
a reference (I've corrected it in my reply to Andrew). Sorry for the
confusion.

Marco
 
A

Andrew Koenig

Victor Bazarov said:
Andrew Koenig wrote:
I think the Standard in 5.2.8 says that the lvalue is returned and that
the lifetime of the object referred to by the lvalue is entire program.
So, why should we be able to make that assumption? Or did you forget the
'not' as in "I don't see why you should _not_ be able..."?

Because I don't see any place in 5.2.8 that prohibits an implementation from
having several distinct objects that represent a given type and returning a
reference to one or another of those objects at whim.

As a more realistic example, consider a class defined in a header file that
is included in several translation units. Where does it say that the
implementation can't create a separate type_info object in each translation
unit?
 
A

Andrew Koenig

Marco Jez said:
"Andrew Koenig" <[email protected]> ha scritto nel messaggio
So, would this map be guaranteed to work as expected?

struct Type_info_cmp
{
bool operator()(const std::type_info *t1, const std::type_info *t2)
const
{
return t1->before(*t2) != 0;
}
};

typedef std::map<const std::type_info*, My_struct, Type_info_cmp>
Type_map;

That's the idea.

[The type_info::before member is in the standard because I proposed it, so I
am confident in knowing its purpose :) ]
 
A

Andrey Tarasevich

Marco said:
...
I would like to use the reference returned by typeid as key in a std::map.
Is it safe to assume that typeid(T) (where T is a type name) will always
return the same reference to the same type_info structure for a given T? My
map would look like this:

typedef std::map<const std::type_info &, ......> Type_map;
...

You cannot use any reference type as a key in an 'std::map' because
reference types are not Assignable, which happens to be a requirement
for 'std::map's key type. Use pointer to 'std::type_info' instead.

Since the C++ standard doesn't explicitly guarantee that 'typeid' always
return a reference to the same 'type_info' object for the same argument
type, you'll have to supply the map with your own comparison predicate,
which will perform "intellegent" comparison of 'type_info' pointers by
comparing the pointed objects using 'std::type_info::before' method.
 
A

Andrey Tarasevich

Victor said:
...
The only doubt I have is that the OP's map is made to have the reference
as the key type. Is it possible? "Key" is required to be assignable.
Are references assignable? I kind of think they are fine, but are they?
...

They are not Assignable of course, by which I mean that any attempt to
syntactically "assign" them will actually be applied to referenced objects.

Firstly, that's definitely not what the OP wants. Secondly, it won't
work because the referenced objects are const-qualified anyway. Thirdly,
it won't work because 'std::type_info' objects are not Assignable
themselves.
 
P

Patrick Kowalzick

Hi Marco,

this is covered as well in the Loki Library. There is a class storing the
pointer to a type_info and comparing via the before function.

A short version looks like this:


class CTypeInfo
{
public:
CTypeInfo(const std::type_info& ti) : pInfo_(&ti) // non-explicit
{
assert(pInfo_);
}
// Access for the wrapped std::type_info
const std::type_info& Get() const
{
assert(pInfo_);
return *pInfo_;
}
// Compatibility functions
bool before(const CTypeInfo & rhs) const
{
assert(pInfo_);
return pInfo_->before(*rhs.pInfo_) != 0;
}
private:
const std::type_info* pInfo_;
};

// Implementation
inline bool operator<(const CTypeInfo& lhs, const CTypeInfo& rhs)
{
return lhs.before(rhs);
}


You can use this class directly in a map then.

Regards,
Patrick
 
A

Andrey Tarasevich

Patrick said:
class CTypeInfo
{
...
// Compatibility functions
bool before(const CTypeInfo & rhs) const
{
assert(pInfo_);

assert(pInfo_ && rhs.pInfo_)

would better fit the general style of using assertions in this code.
 
P

Patrick Kowalzick

would better fit the general style of using assertions in this code.
true :)

Thx,
Patrick
 
T

Tom Widmer

Wouldn't it be legal -- but insane -- for an implementation to have several
type_info objects for a given type, each of which has lifetime equal to the
entire program, and for typeid to to select one at random each time typeid is
invoked?

Perhaps support for dynamic libraries might sometimes lead to the existence of
two typeid objects for the same type.

It does under Windows. Call typeid on the same (non-exported) type in
a DLL and a exe, and the object returned will be different. This is
also why MS's dynamic_cast implementation is so slow - it performs
string comparisons to work out whether two types are the same, whereas
under Unix I think address comparisons are usually sufficient.

Tom
 
P

Patrick Kowalzick

Dear all,
It does under Windows. Call typeid on the same (non-exported) type in
a DLL and a exe, and the object returned will be different. This is
also why MS's dynamic_cast implementation is so slow - it performs
string comparisons to work out whether two types are the same, whereas
under Unix I think address comparisons are usually sufficient.

Is it guaranteed that a type_info object stays "in place" in one compilation
unit? Means, if I have a pointer to type_info object, is this pointer valid
until the end of the program ?

Patrick
 
T

Tom Widmer

Dear all,


Is it guaranteed that a type_info object stays "in place" in one compilation
unit? Means, if I have a pointer to type_info object, is this pointer valid
until the end of the program ?

I think the only exception to the would be if you loaded a DLL, got a
typeid object out of it, and then unloaded the DLL. The standard
guarantees that type_info objects have static storage duration, so
except where a compiler is deviating from the standard (as with DLLs),
type_info object references and pointers are valid for the duration of
the program.

Tom
 
P

Patrick Kowalzick

Hi Tom,
I think the only exception to the would be if you loaded a DLL, got a
typeid object out of it, and then unloaded the DLL. The standard
guarantees that type_info objects have static storage duration, so
except where a compiler is deviating from the standard (as with DLLs),
type_info object references and pointers are valid for the duration of
the program.

Do you have a reference in the standard? I do not doubt your words, but I
searched this before posting and I was not able too find the relevant
paragraphs (like so often).

Thanks,
Patrick
 
A

Andrey Tarasevich

Patrick said:
Do you have a reference in the standard? I do not doubt your words, but I
searched this before posting and I was not able too find the relevant
paragraphs (like so often).

Victor already mentioned 5.2.8 a couple of messages higher in this
thread. 5.2.8/1 explicitly states that the lifetime of the object
referred by the result of 'typeid' extends to the end of the program.
 
P

Patrick Kowalzick

Victor already mentioned 5.2.8 a couple of messages higher in this
thread. 5.2.8/1 explicitly states that the lifetime of the object
referred by the result of 'typeid' extends to the end of the program.

Ups. Sorry.

Regards,
Patrick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top