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
2
votes
2 answers
couldn't deduce template parameter
I am trying to use an API which is similar to the following:
#include
#include
class Base
{
int id;
public:
int get_id()
{
return id;
}
};
class A : public Base
{
};
class B : public Base
{
};
class…

rahman
- 4,820
- 16
- 52
- 86
2
votes
2 answers
Returning rvalue reference from a function that returns Boost optional
As of Boost 1.56, Boost optional supports move semantics. In that case, is the following construct meaningful?
boost::optional getValue()
{
if (value_available) { // value_available is a boolean
return std::move(value); //…

CppNoob
- 2,322
- 1
- 24
- 35
2
votes
1 answer
Boost Spirit Karma multiple optionals
I'm seeing an error that I'm not seeing the resolution to. First, the relevant code:
namespace C {
struct RangeEntry {
size_t byte;
boost::optional bit;
};
struct Range {
RangeEntry firstPart;
…

EHuhtala
- 587
- 3
- 8
2
votes
1 answer
boost::optional with const members
Why doesn't this work?
struct O {
O(int i, int j)
: i(i)
, j(j)
{}
int const i;
int const j;
};
int main(int argc, char** argv)
{
boost::optional i;
i.reset(O(4, 5));
return 0;
}
It seems to be…

nishantjr
- 1,788
- 1
- 15
- 39
1
vote
0 answers
Program crash caused by debug watches on uninitialized boost::optional's content: is it a bug in the adapter, in boost::optional, or where?
The repro are simple and shown in the screencast below.
In essence:
write and compile (g++ -std=c++17 -O0 -g ...) a program (e.g. the one below) featuring an empty boost::optional, say b,
put a breakpoint anywhere,
start the program with the…

Enlico
- 23,259
- 6
- 48
- 102
1
vote
0 answers
How can I view a boost::optional in the CLion debugger?
The built-in GDB in CLion shows a boost::optional as a char array. How can I teach it to show the contents as the real type?

Felix Dombek
- 13,664
- 17
- 79
- 131
1
vote
1 answer
Difference between std::optional and boost::optional when its value is a variant
In embedded programming memory allocations are things that we want to avoid. So using design patterns like state design pattern is cumbersome. If we know how many states we will have then we can use placement new operations. But in this article the…

pallasz88
- 13
- 4
1
vote
1 answer
Remove duplication in equivalent const and non-const members when returning optional reference by value
This answer explains how to remove duplication in equivalent const and non-const member functions which return by reference. It doesn't help, however, in the case where your function returns an optional reference to another type. Is there a solution…
user6429576
1
vote
1 answer
boost optional with another boolean
I am trying to compile something akin to this:
struct Obj { int i = 100; };
boost::optional f(const bool _b)
{
if (_b) return Obj(); else return boost::none;
}
int main()
{
bool run = true;
while (boost::optional retVal =…

Anupam Srivastava
- 789
- 1
- 8
- 25
1
vote
0 answers
Is there a place for anti-std::move in C++
boost::optional can store references, so suppose I want to write a function like this
T frob(const boost::optional x) {
return x.value_or(T{42});
}
This fails on a static_assert that the argument passed to value_or for reference…

unkulunkulu
- 11,576
- 2
- 31
- 49
1
vote
1 answer
View boost::optional content while native debug in Android Studio
I only tried at 2.3.3 studio. Haven't tried new 3.0
Haven't found any plugins that can help
Tried to cast in lldb console. Have some errors there, don't know how to do that properly
So, is there any solutions? How you deal with such problem?

Archont
- 101
- 1
- 9
1
vote
1 answer
Exposing boost::optional via Boost.Python as internal reference or None
I am exposing my C++ classes via Boost.Python. My intention is to expose member variables that are of a user-defined class type with an internal reference. This worked fine until I decided to introduce a member variable of type…

rocketeer
- 21
- 5
1
vote
1 answer
Catching and wrapping exceptions with boost::optional
I'd like to catch exceptions generated by library code and wrap them in boost::optionals (or std::experimental::optional). My code works for the trivial cases, but has difficulty deducing the correct type when calling overloaded functions. I've…

Ryan Stortz
- 11
- 3
1
vote
1 answer
Indirect Member RAII: unique_ptr or optional?
Consider a class with a member that can't be stored directly, e.g., because it does not have a default constructor, and the enclosing class's constructor doesn't have enough information to create it:
class Foo
{
public:
Foo(){} // Default…

Ami Tavory
- 74,578
- 11
- 141
- 185
1
vote
1 answer
Assigning member in optional structure memeber
What is the best style for assigning an optional member of structure?
For example I have a struct:
struct B{
public:
int x;
}
struct A{
public:
boost::optional b;
};
void foo(){
A a;
a.b.x = 10; //Runtime exception because a.b…

Sanich
- 1,739
- 6
- 25
- 43