Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2002-08-20 13:03:25


----- Original Message -----
From: "David Abrahams" <dave_at_[hidden]>
To: "boost" <boost_at_[hidden]>
Cc: "Andy Koenig" <ark_at_[hidden]>; "Herb Sutter" <hsutter_at_[hidden]>
Sent: Tuesday, August 20, 2002 2:32 PM
Subject: [boost] Generic construction

> [snipped]
>
> Finally, there's the issue of default construction. If I want to
> generically declare a default-constructed object of type T, I can't write:
>
> T x;
>
> since builtins (and I presume, enums) won't be initialized. I can't write:
>
> T x();
>
> since that just declares a function. What can/should I write?

Well, at least this last part can be done with this helper wrapper, I think:

template<class T>
class value_initialized
{
  public :

    value_initialized() : x() {}
    operator T& () { return x ; }

  private :
    T x ;
} ;

struct A
{
  A() : x(0) {}
  double x ;
} ;

int main()
{
  value_initialized<double> x_ ; double& x = x_ ;
  cout << x << endl ;

  value_initialized<A> y_ ; A& y = y_ ;
  cout << y.x << endl ;
}

HTH,

Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com


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