C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

typeid operator
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Marco Jez
Guest





PostPosted: Wed Oct 27, 2004 6:29 pm    Post subject: typeid operator Reply with quote



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


Back to top
Andrew Koenig
Guest





PostPosted: Wed Oct 27, 2004 7:16 pm    Post subject: Re: typeid operator Reply with quote



"Marco Jez" <nessuno (AT) nessuno (DOT) com> wrote


Quote:
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.



Back to top
Victor Bazarov
Guest





PostPosted: Wed Oct 27, 2004 7:18 pm    Post subject: Re: typeid operator Reply with quote



Marco Jez wrote:
Quote:
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

Back to top
Victor Bazarov
Guest





PostPosted: Wed Oct 27, 2004 7:34 pm    Post subject: Re: typeid operator Reply with quote

Andrew Koenig wrote:
Quote:
"Marco Jez" <nessuno (AT) nessuno (DOT) com> wrote in message
news:FORfd.214470$35.10108467 (AT) news4 (DOT) tin.it...


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.

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?

Quote:
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

Back to top
Jonathan Turkanis
Guest





PostPosted: Wed Oct 27, 2004 7:55 pm    Post subject: Re: typeid operator Reply with quote


"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote

Quote:
Andrew Koenig wrote:
"Marco Jez" <nessuno (AT) nessuno (DOT) com> wrote in message
news:FORfd.214470$35.10108467 (AT) news4 (DOT) tin.it...


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.

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







Back to top
Marco Jez
Guest





PostPosted: Wed Oct 27, 2004 8:22 pm    Post subject: Re: typeid operator Reply with quote

"Andrew Koenig" <ark (AT) acm (DOT) org> ha scritto nel messaggio
news:8uSfd.787921$Gx4.579909 (AT) bgtnsc04-news (DOT) ops.worldnet.att.net...

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

That's why I've asked... Smile
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.

Quote:
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




Back to top
Marco Jez
Guest





PostPosted: Wed Oct 27, 2004 8:27 pm    Post subject: Re: typeid operator Reply with quote

Quote:
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



Back to top
Andrew Koenig
Guest





PostPosted: Wed Oct 27, 2004 8:34 pm    Post subject: Re: typeid operator Reply with quote


"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote

Quote:
Andrew Koenig wrote:

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.

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?



Back to top
Andrew Koenig
Guest





PostPosted: Wed Oct 27, 2004 8:36 pm    Post subject: Re: typeid operator Reply with quote

"Marco Jez" <guess (AT) none (DOT) it> wrote

Quote:
"Andrew Koenig" <ark (AT) acm (DOT) org> ha scritto nel messaggio

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 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 Smile ]



Back to top
Andrey Tarasevich
Guest





PostPosted: Wed Oct 27, 2004 8:49 pm    Post subject: Re: typeid operator Reply with quote

Marco Jez wrote:
Quote:
...
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.

--
Best regards,
Andrey Tarasevich

Back to top
Andrey Tarasevich
Guest





PostPosted: Wed Oct 27, 2004 8:53 pm    Post subject: Re: typeid operator Reply with quote

Victor Bazarov wrote:
Quote:
...
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.

--
Best regards,
Andrey Tarasevich

Back to top
Patrick Kowalzick
Guest





PostPosted: Wed Oct 27, 2004 9:17 pm    Post subject: Re: typeid operator Reply with quote

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


Back to top
Andrey Tarasevich
Guest





PostPosted: Wed Oct 27, 2004 9:37 pm    Post subject: Re: typeid operator Reply with quote

Patrick Kowalzick wrote:
Quote:
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.

Quote:
return pInfo_->before(*rhs.pInfo_) != 0;
}
...

--
Best regards,
Andrey Tarasevich

Back to top
Patrick Kowalzick
Guest





PostPosted: Wed Oct 27, 2004 10:20 pm    Post subject: Re: typeid operator Reply with quote

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

return pInfo_->before(*rhs.pInfo_) != 0;
}

true :)

Thx,
Patrick



Back to top
Tom Widmer
Guest





PostPosted: Thu Oct 28, 2004 10:21 am    Post subject: Re: typeid operator Reply with quote

On Wed, 27 Oct 2004 13:55:18 -0600, "Jonathan Turkanis"
<technews (AT) kangaroologic (DOT) com> wrote:

Quote:

"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote in message
news:0LSfd.7263$Ae.1053 (AT) newsread1 (DOT) dllstx09.us.to.verio.net...
Andrew Koenig wrote:
"Marco Jez" <nessuno (AT) nessuno (DOT) com> wrote in message
news:FORfd.214470$35.10108467 (AT) news4 (DOT) tin.it...


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.

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.

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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.