> ? the integer that was used to construct the class?
> ! optional of course provides several ways to detect wether or not the variable is initialized. !
!!! yes! the integer that was used to construct the class !!!
I would be delighted (and eternally grateful to you) if you can teach me how I can detect whether the contained value in
optional, if present, is an initialised value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <boost/optional.hpp>
void foo( boost::optional<int> arg )
{
std::cout << std::boolalpha << "does arg hold an initialised integer value? "
/* << what do you want me to do here? */ << '\n' ;
}
int main()
{
int i ; // i is uninitialised
foo(i) ; // how does foo detect that the integer is uninitialised?
int a[5] {} ;
foo( a[-1] ) ; // how does foo detect that this is undefined behaviour?
}
|
> optional is indeed the way to go.
From my, (admittedly poor and limited) understanding, the purpose of
optional is to provide a facility by which a programmer can specify that a value that may or may not be present. That is,
void foo( boost::optional<int> arg ) may be called without specifying a contained integer value.
AFAIK, it provides no mechanism to ensure that:
a. the function must be called with an integer argument (a contained value must be present)
b. that contained value is a value that has been correctly initialised.
As I see it, the futile attempt to use
optional to achieve this purpose is asinine.