Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2002-08-02 10:24:42


----- Original Message -----
From: "Peter Simons" <simons_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, August 02, 2002 11:49 AM
Subject: Re: [boost] 3-state boolean type

> Just curious: Shouldn't the property of a variable being unset or being
> set (and having a value in the latter case) be usable more generally?
> In a project of mine, I used the following template to achieve this:
>
> template <typename varT>
> class resetable_variable
> {
> public:
> typedef varT value_type;
>
> resetable_variable()
> : is_set(false)
> {
> }
> resetable_variable(const value_type& val)
> : value(val), is_set(true)
> {
> }
> resetable_variable<value_type>& operator= (const value_type& rhs)
> {
> value = rhs;
> is_set = true;
> return *this;
> }
> const value_type& data() const throw()
> {
> return value;
> }
> operator const value_type&() const throw()
> {
> return value;
> }
> bool empty() const throw()
> {
> return !is_set;
> }
>
> private:
> value_type value;
> bool is_set;
> };
>
> This was used when parsing configuration files in order to know which
> values had been set in the file and which had not.
>
Yep, I've found this quite useful myself.
Look at the Files Section for 'class optional'. It is conceptually quite
similar to resetable_variable.
I had held formal submission of the optional<> class waiting for a
definitive solution to the 'stack-aligned storage' problem, because
optional<> doesn't default contructs its object when it is uninitialized,
but for this it requires a properly-aligned uninitialized raw storage as
part of itself (to avoid a heap allocation).
Now we do have aligned storage, but I haven't resumed the submission setup
(even though I have incorporated it in my own optional<> code which I use
daily), mainly since it wasn't clear when I first drop the idea here if it
raised sufficient interest.
Now that you bring this up, I think I might brush it up and make a formal
submission.

Nevertheless, a tribool type is useful in it self, beyond this.

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