J
jl_post
Hi,
I was wondering if there was such a thing as an STL container class
that could only hold 0 or 1 instances of an object -- sort of like a
std::vector than can only hold a maximum of one object.
This class would come in handy if I want to create a function/
method that may or may not return a valid value. As a trivial
example, I could create a function that divides two numbers, but only
returns a valid value if the input allows for it:
std::???<double> value = divide(12, 2); // returns a value of 6
std::???<double> value = divide(12, 0); // returns an empty value
This behavior would be similar to Perl's often-used convention of
returning "undef" if a value could not be retrieved or computed.
The reason I want a class like this is because I have seen several
times in C++ code where a value needs to be known if it's set or not.
Often, 0 or -1 is often used as a "not set" indicator, but that
doesn't always work for all values (like temperature values, or
latitude & longitude, which can easily be negative).
Often the workaround used for this is to return a pointer, and if
the pointer is NULL then the value can be assumed to be unset, and if
the pointer is non-NULL then it can be de-referenced to get the real
value, like this:
double *ptr = divide(a, b);
if (ptr)
std::cout << "a/b = " << *ptr << std::endl;
else
std::cout << "No answer computed." << std::endl;
delete ptr;
However, I don't particularly like this approach, as it requires the
programmer to manage the memory (and necessarily know whether the
pointer should be free()d or delete()d).
I suppose I can always write my own container class (that shouldn't
be too hard), or just return a std::vector with zero or one elements
(but then I'd have to clearly document that the return vector will
never have a plural number of elements), but that almost feels
overkill to me, like trying to use a hammer to swat a fly.
So I thought I'd ask if such an STL class existed, so in case it
did it would save me the trouble of using a work-around. But if it
doesn't exist, what work-around would you suggest I use?
Thanks!
-- Jean-Luc
I was wondering if there was such a thing as an STL container class
that could only hold 0 or 1 instances of an object -- sort of like a
std::vector than can only hold a maximum of one object.
This class would come in handy if I want to create a function/
method that may or may not return a valid value. As a trivial
example, I could create a function that divides two numbers, but only
returns a valid value if the input allows for it:
std::???<double> value = divide(12, 2); // returns a value of 6
std::???<double> value = divide(12, 0); // returns an empty value
This behavior would be similar to Perl's often-used convention of
returning "undef" if a value could not be retrieved or computed.
The reason I want a class like this is because I have seen several
times in C++ code where a value needs to be known if it's set or not.
Often, 0 or -1 is often used as a "not set" indicator, but that
doesn't always work for all values (like temperature values, or
latitude & longitude, which can easily be negative).
Often the workaround used for this is to return a pointer, and if
the pointer is NULL then the value can be assumed to be unset, and if
the pointer is non-NULL then it can be de-referenced to get the real
value, like this:
double *ptr = divide(a, b);
if (ptr)
std::cout << "a/b = " << *ptr << std::endl;
else
std::cout << "No answer computed." << std::endl;
delete ptr;
However, I don't particularly like this approach, as it requires the
programmer to manage the memory (and necessarily know whether the
pointer should be free()d or delete()d).
I suppose I can always write my own container class (that shouldn't
be too hard), or just return a std::vector with zero or one elements
(but then I'd have to clearly document that the return vector will
never have a plural number of elements), but that almost feels
overkill to me, like trying to use a hammer to swat a fly.
So I thought I'd ask if such an STL class existed, so in case it
did it would save me the trouble of using a work-around. But if it
doesn't exist, what work-around would you suggest I use?
Thanks!
-- Jean-Luc