A Boost C++ library that provides a container that can represent uninitialized objects of arbitrary type, notably allowing easier definition of functions that might not have a value to return
Questions tagged [boost-optional]
100 questions
6
votes
2 answers
Comparison (<), output (<<) and assignment (=) for boost::optional
I have a few questions about how boost::optional works. Let's first do this:
boost::optional i;
Is i < 3 always equivalent to *i < 3 (and similar for other relational operators)?
Is it correct that the conditions i < 3 and *i < 3 are…

roger.james
- 1,478
- 1
- 11
- 23
5
votes
1 answer
boost::optional deprecated get_value_or
I suspect boost::optional's get_value_or was deprecated because it is unsafe if an rvalue is passed as the default parameter. However, it is occasionally useful to be able to reference the optional value or a default alternative.
Is the following…

Rai
- 1,328
- 11
- 20
5
votes
4 answers
Can optional be implemented as 8-byte object?
Is it possible to implement std::optional such that sizeof(std::optional) == 8 by somehow using that you can store characters in a NAN, see http://en.cppreference.com/w/cpp/numeric/math/nan? Are there implementations that do that? Can it be…

levzettelin
- 2,600
- 19
- 32
5
votes
1 answer
boost::optional and type conversion
I was wondering if there is an elegant way to cast a boost::optional to a boost::optional when B can be constructed from A, albeit explicitely. This works:
# include
class Foo
{
int i_;
public:
explicit Foo(int i) :…

P-Gn
- 23,115
- 9
- 87
- 104
5
votes
3 answers
How to disengage std::experimental::optional?
With Boost I can create an optional in-place with:
boost::optional work = boost::in_place(boost::ref(io_service));
And disengage it with:
work = boost::none;
With C++14 / experimental support, I can instead…

Riot
- 15,723
- 4
- 60
- 67
5
votes
3 answers
What is boost::optional efficiency?
I have the following:
class Obj;
typedef std::map StrMap;
std::map > complexMap;
The thing is that for some entries in complexMap the StrMap will be empty and I won't use it at all, so for…

Subway
- 5,286
- 11
- 48
- 59
5
votes
1 answer
Mixing boost's multi_array and optional with C++11 unique_ptr not working
I've put together a bleeding edge setup with G++ 4.7 (though for the moment I'm still using the boost 1.48 that came with sudo apt-get boost-all-dev on Debian Wheezy).
My code is set up where the logical data structure to use would be…

HostileFork says dont trust SE
- 32,904
- 11
- 98
- 167
4
votes
1 answer
Why is boost::optional not convertable to bool for the purposes of std::is_convertible
I have
auto result = std::is_convertible
< boost::optional
, bool
>::value;
static_assert( result , "task should return bool" );
and it fails to compile. The definition of std::is_convertible is
template< class From, class To >…

bradgonesurfing
- 30,949
- 17
- 114
- 217
4
votes
1 answer
I've written C++ code to treat boost::optional as a range but it doesn't compile
I wish to treat boost::optional as a container that can have zero or one elements in it. Logically I should be able to create an iterator to the container and use boost::for_each on it as well. My attempt is below but it fails to compile. I have…

bradgonesurfing
- 30,949
- 17
- 114
- 217
4
votes
2 answers
boost optional recognizes inheritance?
class Base {};
class Derived : public Base {};
void func(boost::optional &) {}
int main () {
boost::optional x;
func(x);
}
will func accept both optionals: base and derived?

mkmostafa
- 3,071
- 2
- 18
- 47
4
votes
1 answer
Boost Optional implicit conversion to bool?
I'm reading some code and I came across something I do not understand.
Its about testing if a Boost::optional value is initialised or not. It uses the gtest framework which provides the ASSERT_TRUE() macro.
#include "gtest\gtest.h"
void test() {
…

Ventu
- 780
- 1
- 12
- 25
4
votes
1 answer
boost::optional return from function
I am reviewing some production code where a function says it will return a boost::optional, but it just returns a double: e.g.
boost::optional Foo(){
double a = 1.0;
double b = 2.0;
return a + b;
}
Is this acceptable style/are…

frickskit
- 624
- 1
- 8
- 19
4
votes
1 answer
boost::optional reference with boost::variant type
I'm currently writing some code for a game and part of that involves creating a history of the actions that have taken place so far in the game. This history is stored in a vector of state_pair_t's pairs of actions (action_t's) and pointers to the…

shuttle87
- 15,466
- 11
- 77
- 106
3
votes
2 answers
boost::optional vs boost::make_optional
I would like to understand better the difference between creating a boost::optional object using the default constructor:
boost::optional pasta = boost::optional(spaghetti)
or using the make_optional…

giubacchio
- 117
- 1
- 12
3
votes
0 answers
bsoncxx::stdx::optional and mongocxx::stdx::optional
Is there any difference between bsoncxx::stdx::optional and mongocxx::stdx::optional in MongoDB C++ driver?
Do they both inherit boost::optional?
I can't find descriptions about them in the MongoDB C++ Driver API Documentation.

Natalie
- 31
- 2