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 

keeping a pointer to typeinfo?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
John Torjo
Guest





PostPosted: Fri Jul 16, 2004 11:17 am    Post subject: keeping a pointer to typeinfo? Reply with quote



Hi all,

Heres's what the standard says:
[expr.typeid] 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. The
lifetimeof the object referred to by the lvalue extends to the end of
the program. Whether or not the destructor is called for the type_info
object at the end of the program is unspecified.

Quick question:
If I read this correctly, I can hold a pointer to dereferencing a
typeid(), and that pointer will last throughout the program. Am I
correct?
Example:
some_class * p = ...;

// this will last throughout the program
const std::type_info * info = *typeid(*p);


Thanks.

Best,
John


John Torjo
Freelancer
-- [email]john (AT) torjo (DOT) com[/email]

Contributing editor, C/C++ Users Journal
-- "Win32 GUI Generics" -- generics & GUI do mix, after all
-- http://www.torjo.com/win32gui/

Professional Logging Solution for FREE
-- http://www.torjo.com/code/logging.zip (logging - C++)
-- http://www.torjo.com/logview/ (viewing/filtering - Win32)
-- http://www.torjo.com/logbreak/ (debugging - Win32)
(source code available)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
tom_usenet
Guest





PostPosted: Mon Jul 19, 2004 5:23 pm    Post subject: Re: keeping a pointer to typeinfo? Reply with quote



On 16 Jul 2004 07:17:27 -0400, [email]jtorjo (AT) yahoo (DOT) com[/email] (John Torjo) wrote:

Quote:
Hi all,

Heres's what the standard says:
[expr.typeid] 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. The lifetimeof the object
referred to by the lvalue extends to the end of the program. Whether or
not the destructor is called for the type_info object at the end of the
program is unspecified.

Quick question:
If I read this correctly, I can hold a pointer to dereferencing a
typeid(), and that pointer will last throughout the program. Am I
correct?
Example:
some_class * p = ...;

// this will last throughout the program const std::type_info * info =
*typeid(*p);

You want:
const std::type_info* info = &typeid(*p); That pointer is now valid for the
duration of the program.

Tom

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Torjo
Guest





PostPosted: Tue Jul 20, 2004 8:13 am    Post subject: Re: keeping a pointer to typeinfo? Reply with quote



tom_usenet <tom_usenet (AT) hotmail (DOT) com> wrote

Quote:
On 16 Jul 2004 07:17:27 -0400, [email]jtorjo (AT) yahoo (DOT) com[/email] (John Torjo) wrote:

Hi all,

Heres's what the standard says:
[expr.typeid] 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. The lifetimeof the object
referred to by the lvalue extends to the end of the program. Whether or
not the destructor is called for the type_info object at the end of the
program is unspecified.

Quick question:
If I read this correctly, I can hold a pointer to dereferencing a
typeid(), and that pointer will last throughout the program. Am I
correct?
Example:
some_class * p = ...;

// this will last throughout the program const std::type_info * info =
*typeid(*p);

You want:
const std::type_info* info = &typeid(*p); That pointer is now valid for the
duration of the program.


Of course, that's what I meant Wink these typos ;)

(basically, only after hitting the "Post message - No preview" I
realized the mistke)

Thanks.

Best,
John



John Torjo
Freelancer
-- [email]john (AT) torjo (DOT) com[/email]

Contributing editor, C/C++ Users Journal
-- "Win32 GUI Generics" -- generics & GUI do mix, after all
-- http://www.torjo.com/win32gui/

Professional Logging Solution for FREE
-- http://www.torjo.com/code/logging.zip (logging - C++)
-- http://www.torjo.com/logview/ (viewing/filtering - Win32)
-- http://www.torjo.com/logbreak/ (debugging - Win32)
(source code available)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
tom_usenet
Guest





PostPosted: Tue Jul 20, 2004 6:20 pm    Post subject: Re: keeping a pointer to typeinfo? Reply with quote

On 20 Jul 2004 04:13:55 -0400, [email]jtorjo (AT) yahoo (DOT) com[/email] (John Torjo) wrote:

Quote:
tom_usenet <tom_usenet (AT) hotmail (DOT) com> wrote

On 16 Jul 2004 07:17:27 -0400, [email]jtorjo (AT) yahoo (DOT) com[/email] (John Torjo) wrote:

Hi all,

Heres's what the standard says:
[expr.typeid] 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. The lifetimeof the object
referred to by the lvalue extends to the end of the program. Whether or
not the destructor is called for the type_info object at the end of the
program is unspecified.

Quick question:
If I read this correctly, I can hold a pointer to dereferencing a
typeid(), and that pointer will last throughout the program. Am I
correct?
Example:
some_class * p = ...;

// this will last throughout the program const std::type_info * info =
*typeid(*p);

You want:
const std::type_info* info = &typeid(*p); That pointer is now valid for the
duration of the program.


Of course, that's what I meant Wink these typos Wink

One thing I should have mentioned is that &typeid(T) isn't guaranteed
always to return the same pointer; in particular, it does on Unix with
it's shared library system, it might not on Windows with it's
independent DLL system.

Tom

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David Abrahams
Guest





PostPosted: Tue Jul 20, 2004 9:40 pm    Post subject: Re: keeping a pointer to typeinfo? Reply with quote

tom_usenet <tom_usenet (AT) hotmail (DOT) com> writes:

Quote:
On 20 Jul 2004 04:13:55 -0400, [email]jtorjo (AT) yahoo (DOT) com[/email] (John Torjo) wrote:

tom_usenet <tom_usenet (AT) hotmail (DOT) com> wrote

On 16 Jul 2004 07:17:27 -0400, [email]jtorjo (AT) yahoo (DOT) com[/email] (John Torjo) wrote:

Hi all,

Heres's what the standard says:
[expr.typeid] 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. The lifetimeof the object
referred to by the lvalue extends to the end of the program. Whether or
not the destructor is called for the type_info object at the end of the
program is unspecified.

Quick question:
If I read this correctly, I can hold a pointer to dereferencing a
typeid(), and that pointer will last throughout the program. Am I
correct?
Example:
some_class * p = ...;

// this will last throughout the program const std::type_info * info =
*typeid(*p);

You want:
const std::type_info* info = &typeid(*p); That pointer is now valid for the
duration of the program.


Of course, that's what I meant Wink these typos ;)

One thing I should have mentioned is that &typeid(T) isn't guaranteed
always to return the same pointer; in particular, it does on Unix with
it's shared library system, it might not on Windows with it's
independent DLL system.

It's easy to make various Unices including G++/Linux display a similar
behavior if you use dlopen(). In fact, two typeid(T)s from different
shared libraries can compare unequal for the same T.

:vP

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Torjo
Guest





PostPosted: Wed Jul 21, 2004 12:04 pm    Post subject: Re: keeping a pointer to typeinfo? Reply with quote

Quote:

Of course, that's what I meant Wink these typos ;)

One thing I should have mentioned is that &typeid(T) isn't guaranteed
always to return the same pointer; in particular, it does on Unix with

That should not count. What I want is, by having two typeinfo
pointers, to be able to say:

*p1 == *p2

Quote:
it's shared library system, it might not on Windows with it's
independent DLL system.

It's easy to make various Unices including G++/Linux display a similar
behavior if you use dlopen(). In fact, two typeid(T)s from different
shared libraries can compare unequal for the same T.

:(

Luckly, this should not be my case. Specifically, I only want it for
Win32.
What I want to achieve is something similar to this:

struct employee { ... };
struct department { ... };

employee e;
department d;

corresp c;
c.add_var(e); // template function
c.add_var(d);

// here, I want to "bind" this to the variable "d"
c.add_corresp(&department::department_id, ID_dep);
// same here
c.add_corresp(&department::department_name, ID_dep_name);
// here, bind this to variable "e"
c.add_corresp(&employee::employee_name, ID_name);

Best,
John



John Torjo
Freelancer
-- [email]john (AT) torjo (DOT) com[/email]

Contributing editor, C/C++ Users Journal
-- "Win32 GUI Generics" -- generics & GUI do mix, after all
-- http://www.torjo.com/win32gui/

Professional Logging Solution for FREE
-- http://www.torjo.com/code/logging.zip (logging - C++)
-- http://www.torjo.com/logview/ (viewing/filtering - Win32)
-- http://www.torjo.com/logbreak/ (debugging - Win32)
(source code available)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
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.