Boost logo

Boost :

From: Arkadiy Vertleyb (vertleyb_at_[hidden])
Date: 2005-05-30 23:25:22


"Arkadiy Vertleyb" <vertleyb_at_[hidden]> wrote

> "Alexander Nasonov" <alnsn-boost_at_[hidden]> wrote
>
> > BOOST_TYPEOF( Z() ), where Z is a class type, gives different results in
> native
> > and compliant modes, at least on gcc 3.2.2.
> >
> > #include <boost/typeof/typeof.hpp>
> > #include <boost/mpl/assert.hpp>
> > #include <boost/type_traits/is_same.hpp>
> >
> > #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
> >
> > struct Z {};
> >
> > BOOST_TYPEOF_REGISTER_TYPE(Z)
> >
> >
> > int main()
> > {
> > typedef Z(F)(); // function in native mode
> > typedef BOOST_TYPEOF( Z() ) fn_or_class;
> >
> > // Which line should be commeneted?
> > BOOST_MPL_ASSERT(( boost::is_same<fn_or_class,Z> ));
> > BOOST_MPL_ASSERT(( boost::is_same<fn_or_class,F> ));
> > }
>
> Well, the meaning of Z() is ambiguous. It can be
>
> 1) a default-constructed temporary object of class Z;
> 2) a type, which is a function that accepts no parameters and returns Z.
>
> A native gcc typeof can handle types, as well as objects: __typeof__(int)
is
> valid in g++. So the compiler has a choice: either interpret Z() as a
type
> or an object, and apparently chooses the easier one (I think the right
thing
> would be to complain about the ambiguity).
>
> In case of emulation the argument of typeof is passed to a function, so it
> is unambiguously interpreted as an object.
>
> I don't think we can do much about it except mention in the docs that we
> can't handle types in the emulation mode.

OTOH, passing the parameter through something like:

template<class T>
T make (const T&);

seems to be able to convince GCC that this is indeed an expression. So, if
we define the BOOST_TYPEOF (in native mode) something like this
(mpl::identity insures consistency of typename usage):

#define BOOST_TYPEOF(x) mpl::identity<__typeof__(make(x))>::type

We should be able to achieve consistency between native and emulation modes.

(It also looks very similar to what David suggested in order to workaround
some ICEs in GCC, see http://lists.boost.org/boost/2005/05/26880.php)

Regards,
Arkadiy


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk