Boost logo

Boost :

From: Niels Dekker - mail address until 2008-12-31 (nd_mail_address_valid_until_2008-12-31_at_[hidden])
Date: 2008-01-07 08:09:54


For the convenience of the end users, I would like to add a little class
to value_init.hpp, called initialized_value, having a conversion
operator as its one and only member. Because it's just an extra feature
of value_init, and Fernando Cacciola (the owner of value_init) already
agrees that this class is a useful addition, I don't think an officially
review of the class is necessary. But please tell me if you think
otherwise.

Details:

The following syntax should initialize an object in a generic way,
assuming that its type T is CopyConstructible:
    T obj1 = T();

Unfortunately this object may *not* be initialized properly, because of
value-initialization related compiler issues of various C++
implementations, including MSVC, Borland, and GCC. (Boost ticket #1217,
#1459, and #1491.) Fernando Cacciola and I have recently reimplemented
boost::value_initialized, to work around those issues. (value_init.hpp,
release branch, changeset [42501].) So the next release version of
boost::value_initialized can be used to properly initialize an object,
even in the presence of those compiler issues, as follows:
    T obj2 = get( value_initialized<T>() );

Still this way of initializing an object is rather verbose, and it has a
form of code duplication, because it mentions the type of the object (T)
twice. Within user code, the type may of course look more like
Namespace::Template<Arg>::Type. Therefore I would like to add a class,
boost::initialized_value, supporting the following:
    T obj3 = initialized_value();

The class would be implemented as follows:

namespace boost {

  class initialized_value
  {
    public :
      template <class T> operator T() const
      {
        return get( value_initialized<T>() );
      }
  };
}

Its conversion operator would provide an appropriate value-initialized
object for any CopyConstructible type T.

Any comments are welcome. If it's okay, I would like to add it to the
trunk version of value_init.hpp, next week.

Kind regards,

--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center

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