Boost logo

Boost :

Subject: [boost] [utility] A fix for the const issue of value_initialized
From: Niels Dekker - mail address until 2010-10-10 (niels_address_until_2010-10-10_at_[hidden])
Date: 2009-02-17 14:04:10

The boost::value_initialized<T> utility has a major issue regarding
const-correctness: It has a const-qualified conversion operator,
returning a non-const reference to the T object it owns. I have already
created a ticket, <>, and
mailed Fernando Cacciola (the author of value_initialized) on this
issue. He agrees that it should be fixed, and he allows me to do so. The
patch I've attached to the ticked replaces the current conversion
operator by two of them, overloaded for const and non-const access:

  operator T const &() const { return this->data(); }
  operator T&() { return this->data(); }

The patch also fixes value_initialized::data() in a similar way. Please
let me know if you have any problem with the patch. Otherwise I'd like
to apply it to the trunk by the end of the week:

Note that the issue has been discussed here already five years ago: At that time,
some non-conforming compilers couldn't deal with conversion operators
overloaded for const and non-const access. But now I've tested the
value_initialized patch on a few compilers (MSVC 2003, MSVC 2008,
CodeGear), and they all succeeded. After the commit, I'll keep a close
look at the regression page, of course.

Kind regards, Niels

Niels Dekker
Scientific programmer at LKEB, Leiden University Medical Center

Boost list run by bdawes at, gregod at, cpdaniel at, john at