Boost logo

Boost :

From: Vesa Karvonen (vesa.karvonen_at_[hidden])
Date: 2001-06-15 09:45:48


How about using an applicative template to supply the initialization
parameter:

    template
    < class value_type
    , class initializer
>
    struct initialized
    { typedef value_type
        value_type;

      // constructor invokes the initializer
      initialized() : m_value(initializer::apply()) {}

      // ...desired convenience operators...
    private:
      value_type
        m_value;
    };

    // An initializer
    template
    < int value
>
    struct initializer_int
    { static int apply() {return value;}
    };

The user could then write:

    initialized<int, initializer_int<5> >

However, this is not the end of the story. The applicative template
approach would make it possible to perform an arbitrary computation in
order to calculate the default. Even a reasonable compiler can completely
eliminate any run-time overhead compared to the direct template parameter
approach.

In my opinnion, the "convenience operators" should be configurable. The
semantics and availability of
- get/set,
- conversions,
- address of operator and
- other built-in operators
should be configurable.

This is starting to sound like a Property concept.

----- Original Message -----
From: "Aleksey Gurtovoy" <alexy_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Thursday, June 14, 2001 5:16
Subject: RE: [boost] A little utility class I'd like to contribute to boos
t

> David Abrahams wrote:
> > I think it's nice, but would prefer to call the template
> > "initialized".
>
> Me too (in fact, that's exactly how something like this is called in our
> code :).
>
> > Also, I note that it /only/ works with built-in
> > integer types. What about pointers and floating types? I think
> > these deserve an "initialized" type as well.
>
> Yes, it should be something like this:
>
> template< typename T
> , typename non_type_parameter<T>::type Default
> = typename non_type_parameter<T>::type()
> >
> struct initialized : boost::mpl::select_type<
> boost::is_pointer<T>::value
> , initialized_ptr<T>
> , initialized_arithmetic<T>
> >::type
> {
> // ...
> };
>
> where 'non_type_parameter<double>::type' (or 'float') gives you
something
> like 'long', so at least you can have an integer-based initialization:
>
> initialized<double> d; // ok
> initialized<double, 15> d1; // ok
> // initialized<double, 15.5> d1; // error
>
> Also, overloading of unary operator& to make it return an address of
> internal object might make sense too.
>
>
> Aleksey
>
> To unsubscribe, send email to:
<mailto:boost-unsubscribe_at_[hidden]>
>
>
> Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/
>
>
>


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