a template to extend types to support something like SQL NULL
hi
i have an int. but it should also support a "undefined" state like SQL
NULL. most times i can use -1 for this. but there are also data types
without a reasonable undefined value. maybe float or double. or maybe -1
is perfectly legal.
so i used my own smartptr class (which i will replace by
boost::shared_ptr since it is better and compatible to libs from
strangers) to fix the problem as follows:
shared_ptr<int> a(new int(10));
shared_ptr<int> b; // has the value "undefined" like SQL NULL.
now i can do checks like
if ( ! b ) printf("%d", *b);
now my problem was that several places in my program access the same int
instance. they can assign values, and then other parts of the program
can read these values. that's good behaviour.but i cannot set the object
to "undefined". see why:
void foo (shared_ptr<int> a)
{
*a = 10; // <-- ok! this works! everybody now has the 10!
}
void errorfoo (shared_ptr<int> a)
{
a.reset(); // <-- this does of course not work, since only my local
// copy of a is now SQL NULL.
}
so i restructured my shared_ptr to make it able to hold this special SQL
NULL state also. this way i got really awkward semantics though. i don't
go in details here, but in my shared_ptr the errorfoo function works as
expected. (the function is not called "reset" then).
but i got severe problems in typecast semantics of my modified
shared_ptr, so i decided to dump this beast before anyone else has to
see it :-)))
i thought that maybe it's not a good idea to unite smartpointer with SQL
NULL facility. maybe it is better to write:
shared_ptr
is there such a nullable type extender beast in boost somewhere? or do i have to invent it?
I believe you what you want is in boost.optional: http://www.boost.org/libs/optional/doc/optional.html Jeff
Erik Thiele wrote:
hi
i have an int. but it should also support a "undefined" state like SQL NULL. most times i can use -1 for this. but there are also data types without a reasonable undefined value. maybe float or double. or maybe -1 is perfectly legal.
Have you considered boost.optional? Regards, -- Joel de Guzman http://www.boost-consulting.com http://spirit.sf.net
participants (3)
-
Erik Thiele
-
Jeff Garland
-
Joel de Guzman