dereferencing type-punned pointer in function_base under gcc-4.3

I'm trying to make sure my code compiles under gcc-4.3 before that compiler comes out. I'm currently stuck on an error it reports in boost, version 1.34.1 (which I downloaded seconds ago; error was also present in 1.34.0): In file included from ../boost/function/detail/prologue.hpp:16, from ../boost/function.hpp:22, from ../boost/iterator/transform_iterator.hpp:10, from [my code]: ../boost/function/function_base.hpp: In member function 'const std::type_info& boost::function_base::target_type() const': ../boost/function/function_base.hpp:485: error: dereferencing type-punned pointer will break strict-aliasing rules I don't understand how there's any type-punning going on at line 485, although to my untrained eye it looks a lot like it's returning a ref to a temporary. A couple lines later looks only very slightly fishier, but still shouldn't be breaking any aliasing rules. So I leave it to the experts to figure out what's going on here. -- BenoƮt

On Oct 21, 2007, at 1:34 PM, Benoit Hudson wrote:
I'm trying to make sure my code compiles under gcc-4.3 before that compiler comes out. I'm currently stuck on an error it reports in boost, version 1.34.1 (which I downloaded seconds ago; error was also present in 1.34.0):
In file included from ../boost/function/detail/prologue.hpp:16, from ../boost/function.hpp:22, from ../boost/iterator/transform_iterator.hpp:10, from [my code]: ../boost/function/function_base.hpp: In member function 'const std::type_info& boost::function_base::target_type() const': ../boost/function/function_base.hpp:485: error: dereferencing type-punned pointer will break strict-aliasing rules
I don't understand how there's any type-punning going on at line 485, although to my untrained eye it looks a lot like it's returning a ref to a temporary. A couple lines later looks only very slightly fishier, but still shouldn't be breaking any aliasing rules. So I leave it to the experts to figure out what's going on here.
I'm pretty certain the compiler is wrong here, although we would need to distill this into a much simpler test case before reporting it to GCC. There is no type-punning going on, because the "manager" will always set the const_obj_ptr to a pointer to a const std::type_info object, and then we extract the pointer as a std::type_info const*. There also is no temporary involved here, because typeid always returns an lvalue. The object that the lvalue refers to lives until the end of the program. I think we might be able to help the compiler by adding type.const_obj_ptr = 0; prior to the call to vtable->manager in the target_type function. - Doug

I went through and reduced it just a bit: cat > reduced.cpp << EOF namespace std { class type_info { }; } struct Cow { const std::type_info& moo() { return typeid(void); } }; EOF gcc4.3 -O2 -Wall reduced.cpp
participants (2)
-
Benoit Hudson
-
Douglas Gregor