Boost logo

Boost :

From: Arkadiy Vertleyb (vertleyb_at_[hidden])
Date: 2005-05-21 18:00:16


"David Abrahams" <dave_at_[hidden]> wrote

> I believe I have substantially simplified the implementation of typeof
> and fixed the vc8 compatibility problems. See the enclosed
> typeof_impl.hpp

Right. That's exactly the simplification I meant in my previous post.
Unfortunately it means at() is instantiated BOOST_TYPEOF_LIMIT_SIZE times,
whereas with more complicated expression it is instantiated only n times,
where n is the actual size of the encoded vector. I introduced this
complexity about half a year ago, and it cut the time of my tests more than
in half...

> I am a little concerned about the implementation. When I saw the
> complexity of what the BOOST_TYPEOF macro expanded into, I was a bit
> shocked. Aside from what I addressed, the fact that the expression
> gets repeated 100 times was really surprising to me -- it seems to me
> that asking the compiler to evaluate the type of the expression over
> and over could get to be quite expensive.

But this is on average seems to be compensated by less times the template is
instantiated.

> The simplification I've
> made results in the expression being repeated "only" 50 times. Is it
> possible to do better?

I don't believe it's possible with conventional metaprogramming. The
problem is that, in order to calculate the size of the vector at compile
time, we have to pass the expression inside a metafunction, which doesn't
seem possible. Therefore we have to do it at preprocessing time, which
means BOOST_TYPEOF_LIMIT_SIZE times. This is the price of crossing
boundaries.

> Also, on a stylistic note, the paucity of whitespace in
>
> namespace boost{namespace type_of{
>
> seems a bit draconian (yes, I'm exercising my vocabulary today ;->).

Well, there are two whitespaces :-)

Why? Does it make it unreadable?

Regards,
Arkadiy

----------------------------------------------------------------------------

----
> // Copyright (C) 2004 Arkadiy Vertleyb
> // Use, modification and distribution is subject to the Boost Software
> // License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt)
>
> #ifndef BOOST_TYPEOF_TYPEOF_IMPL_HPP_INCLUDED
> #define BOOST_TYPEOF_TYPEOF_IMPL_HPP_INCLUDED
>
> #include <boost/mpl/size_t.hpp>
> #include <boost/preprocessor/repetition/enum.hpp>
> #include <boost/typeof/encode_decode.hpp>
> #include <boost/mpl/int.hpp>
>
> #ifdef BOOST_TYPEOF_USE_MPL_VECTOR
> #   include <boost/mpl/vector.hpp>
> #   include <boost/mpl/size.hpp>
> #   include <boost/mpl/at.hpp>
> #   include <boost/mpl/begin_end.hpp>
> #   include <boost/mpl/push_back.hpp>
> #   include <boost/typeof/limit_size.hpp>
> #   define BOOST_TYPEOF_VECTOR(n) BOOST_PP_CAT(boost::mpl::vector, n)
> #else
> #   include <boost/typeof/vector.hpp>
> #   define BOOST_TYPEOF_VECTOR(n) BOOST_PP_CAT(boost::type_of::vector, n)
> #endif
>
> namespace boost{namespace type_of{
>
> template<int pos, class T>
> struct at_
> {
>     enum {
>         size = mpl::size<
>                  typename encode_type<BOOST_TYPEOF_VECTOR(0)<>, T>::type
>         >::type::value
>     };
>
>     enum { n = (pos < size) ? pos : 0 };
>
>     typedef char(&type)[
>         mpl::at_c<
>             typename encode_type<BOOST_TYPEOF_VECTOR(0)<>, T>::type
>           , n
>         >::type::value
>     ];
> };
>
>
> template <int pos, class T>
> static
> typename at_<pos,T>::type
> at(const T&);
> }}
>
>
> #define BOOST_TYPEOF_TYPEITEM(z, n, expr)\
>     boost::mpl::size_t<sizeof(boost::type_of::at<n>(expr))>
>
> #define BOOST_TYPEOF(Expr)
\
>     boost::type_of::decode_type<
\
>         boost::mpl::begin<
\
>             BOOST_TYPEOF_VECTOR(BOOST_TYPEOF_LIMIT_SIZE)<
\
>                 BOOST_PP_ENUM(BOOST_TYPEOF_LIMIT_SIZE,
BOOST_TYPEOF_TYPEITEM, Expr) \
>             >
\
>         >::type
\
>     >::type
>
> #define BOOST_TYPEOF_TPL(Expr)
\
>     typename boost::type_of::decode_type<
\
>         typename boost::mpl::begin<
\
>             BOOST_TYPEOF_VECTOR(BOOST_TYPEOF_LIMIT_SIZE)<
\
>                 BOOST_PP_ENUM(BOOST_TYPEOF_LIMIT_SIZE,
BOOST_TYPEOF_TYPEITEM, Expr) \
>             >
\
>         >::type
\
>     >::type
>
> #endif//BOOST_TYPEOF_COMPLIANT_TYPEOF_IMPL_HPP_INCLUDED
>
----------------------------------------------------------------------------
----
>
> --
> Dave Abrahams
> Boost Consulting
> www.boost-consulting.com
>
----------------------------------------------------------------------------
----
> _______________________________________________
> Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost

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