Topic: are type_infos available during initialization of static objects ?


Author: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/06/09
Raw View
"Paul D. DeRocco" <pderocco@ix.netcom.com> writes:

 |> Christian Millour wrote:
 |> >
 |> > Hi all. I have a problem with the following snippet:
 |> >
 |> > #include <typeinfo>
 |> > #include <cstdio>
 |> >
 |> > struct bar {};
 |> >
 |> > template <typename T>
 |> > struct foo { foo() { puts(typeid(T).name()); }};
 |> >
 |> > foo<bar> f;
 |> >
 |> > int main() { return 0; }
 |> >
 |> > the program segfaults (g++ 2.7.2). The debugger seems to indicate
 |> > that the type_info instance returned by typeid(T) has not been
 |> > initialized when I use it (its vptr is 0).
 |> >
 |> > I seem to have run in a `order of construction of static objects'
 |> > problem. Does the standard have anything to say on this subject
 |> > (such as making sure that type_info instances are constructed
 |> > before user statics and globals) ?
 |>
 |> I don't see any specific mention of this, but it does say in 5.2.8.1
 |> that "the result of a typeid expression is an lvalue of type const
 |> std::type_info" and that its lifetime extends to the end of the program.
 |> It doesn't say that its lifetime doesn't start at the beginning of the
 |> program, so I think we should have a right to assume it exists as a
 |> valid object even during static initialization.

I think you're making a dangerous leap, although I don't think that it
matters.  As far as I can see, there is certainly no requirement that
the type_info object exist before dynamic initialization; what is
required is that a typeid expression return a valid (fully constructed)
typeid.   Whether it was constructed before dynamic initialization, or
"on the fly" when it was first needed, is not defined.

That said, of course, if a tree falls in a forest...  Since by
definition, there is no way for a conforming program to tell,
considering it to be constructed before dynamic initialization doesn't
matter.

 |> I'm surprised that type_info objects start out with null vptrs. I would
 |> think that any implementation would be able to implement them as part of
 |> the load-time image (Borland does), since they shouldn't need explicit
 |> construction.

This would be one possibility.  Another would be to use something like
the singleton pattern.

--
James Kanze      home:     kanze@gabi-soft.fr        +33 (0)1 39 55 85 62
                 office:   kanze@vx.cit.alcatel.fr   +33 (0)1 69 63 14 54
GABI Software, Sarl., 22 rue Jacques-Lemercier, F-78000 Versailles France
     -- Conseils en informatique industrielle --
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Christian Millour <chris161@club-internet.fr>
Date: 1997/06/02
Raw View
Hi all. I have a problem with the following snippet:

#include <typeinfo>
#include <cstdio>

struct bar {};

template <typename T>
struct foo { foo() { puts(typeid(T).name()); }};

foo<bar> f;

int main() { return 0; }


the program segfaults (g++ 2.7.2). The debugger seems to indicate
that the type_info instance returned by typeid(T) has not been
initialized when I use it (its vptr is 0).

I seem to have run in a `order of construction of static objects'
problem. Does the standard have anything to say on this subject
(such as making sure that type_info instances are constructed
before user statics and globals) ?

TIA.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1997/06/03
Raw View
Christian Millour wrote:
>
> Hi all. I have a problem with the following snippet:
>
> #include <typeinfo>
> #include <cstdio>
>
> struct bar {};
>
> template <typename T>
> struct foo { foo() { puts(typeid(T).name()); }};
>
> foo<bar> f;
>
> int main() { return 0; }
>
> the program segfaults (g++ 2.7.2). The debugger seems to indicate
> that the type_info instance returned by typeid(T) has not been
> initialized when I use it (its vptr is 0).
>
> I seem to have run in a `order of construction of static objects'
> problem. Does the standard have anything to say on this subject
> (such as making sure that type_info instances are constructed
> before user statics and globals) ?

I don't see any specific mention of this, but it does say in 5.2.8.1
that "the result of a typeid expression is an lvalue of type const
std::type_info" and that its lifetime extends to the end of the program.
It doesn't say that its lifetime doesn't start at the beginning of the
program, so I think we should have a right to assume it exists as a
valid object even during static initialization.

I'm surprised that type_info objects start out with null vptrs. I would
think that any implementation would be able to implement them as part of
the load-time image (Borland does), since they shouldn't need explicit
construction.

--

Ciao,
Paul

(Please remove the extra dots from the return address,
which has been altered to foil junk mail senders.)
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]