Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-01-31 13:40:37


[Please allow me to suggest that you turn on line wrapping in your
mailer. Most of your paragraphs were just one line]

"Jonathan Wang" <babysloth_at_[hidden]> writes:

> Hi,
>
> There's an nice implementation of the NTP in boost-sandbox, which
> follows the idea in the book "C++ Templates". But we have to repeat
> the definitions the "feature_is" classes with virtual inheritance
> and some typedefs all the time. I wonder if we can make it eaiser.
>
> Taking the test "car" class in the boost-sandbox.ntp as an example,
> I'd like just to write:
>
> // requires no definitions
> template <typename T> struct Model_is;
> template <typename T> struct Color_is;
> template <typename T> struct Seats_is;
> template <typename T> struct Transmission_is;
>
> then we can easily implement the "car" class like this:
>
> template <class Model = ntp::nil,
> class Color = ntp::nil,
> class Seats = ntp::nil,
> class Transmission = ntp::nil>
> class car {
> // the default template arguments
> typedef typename boost::mpl::vector4<
> Model_is<gl>, Color_is<red>, Seats_is<leather>, Transmission_is<automatic>
> > default_features;
>
> // all setters
> typedef typename boost::mpl::vector4<Model, Color, Seats, Transmission> setters;
>
> // update the default arguments
> typedef typename ntp::update<default_features, setters>::type features;
>
> // the named parameters are accessible through the 'features' type
> typedef typename ntp::extract<features, Model_is>::type model;
> typedef typename ntp::extract<features, Color_is>::type color;
> typedef typename ntp::extract<features, Seats_is>::type seats;
> typedef typename ntp::extract<features, Transmission_is>::type transmission;
>
> //...
> };
>
> The default features(types) are stored in a type-vector, and we can
> use boost.mpl to update the default features into use-defined ones,
> and to extract the updated paramters from type-vector. Then we can
> use NTPs later. And even can we mix the NTPs and the ordinary
> paramters, just to use ntp::update2 instead of ntp::update.

Nifty, Nifty, Nifty!!!

> The problems I've come across mainly on the lambda support of mpl. I
> tried to use boost::mpl::fold to implement the "update" template,
> but failed to compile with bcc 5.6(all is ok with gcc 3.2). And as
> the template template parameters are used, I wonder if any
> workaround for VC6. The alternative should be more convenient, but
> might take more time to compile, and less portable.

Broken compilers like VC and Borland require a little bit of help in
user-defined metafunctions.

See
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/boost/boost/boost/python/detail/indirect_traits.hpp?rev=HEAD&content-type=text/plain
and look for uses of BOOST_MPL_AUX_LAMBDA_SUPPORT.

>
> I've tried an implementation, and have uploaded it to http://www.c-view.org/ntp.zip. It's beeen tested with Borland C++ Compiler 5.6 and gcc 3.2. The files in the zip package are:
>
> test.cpp example of NTPs
> test2.cpp example of mixed use of NTPs and ordinary paramters
> ntp.hpp NTP
> ntp2.hpp for mixed use of NTPs and ordinary paramters

Very exciting!

> Thanks very much for review.
>
> The Chinese Spring Festival is coming. I wish every booster well and
> happy in the new year. And a Chinese congratulatory saying:
>
> Lucky Money!

Thank you very much!

-- 
                       David Abrahams
   dave_at_[hidden] * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

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