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
13
votes
1 answer
Difference between boost optional and std::experimental optional assignment
Usually when a function returns boost::optional I've seen a lot of people returning an empty brace {} to designate an empty value, that works fine and is shorter than returning boost::none.
I tried to do something similar to empty a…

dlavila
- 1,204
- 11
- 25
11
votes
1 answer
init boost::optional of non-copyable object
What should I do to initialize boost::optional< T > if underlying type T is non-default constructible, non-copyable/moveable, but one's instance still can exist?
Is it forbidden for boost::optional by any semantic reasons to have some member…

Tomilov Anatoliy
- 15,657
- 10
- 64
- 169
11
votes
2 answers
Can I return an optional from a constexpr function?
Can I return an optional from a constexpr function?
Why?
If yes, how does it work?
I'm interested in both boost::optional and std::optional. Do they behave the same?

gnzlbg
- 7,135
- 5
- 53
- 106
9
votes
2 answers
Use boost::optional together with boost::adaptors::indirected
I am trying to compile the following code:
#include
#include
#include
#include
#include
#include
#include…

Mathias Soeken
- 1,293
- 2
- 8
- 22
9
votes
4 answers
Why use boost::optional when I can return a pointer
If I have a find function that can sometimes fail to find the required thing, I tend to make that function return a pointer such that a nullptr indicates that the thing was not found.
E.g.
Student* SomeClass::findStudent(/** some criteria. */)
If…

nappyfalcon
- 909
- 1
- 10
- 17
9
votes
3 answers
Can't see boost::optional contents when debugging with Visual Studio
If I try to look at the variable directly, I see a ? sign. If I create a watch calling the is_initialized function, I get the following error:
CXX0033: Error: error in OMF type information
I didn't find much info about this error related to Boost…

dario_ramos
- 7,118
- 9
- 61
- 108
8
votes
4 answers
How do I prevent boost::optional from being constructed erroneously with 0?
boost::optional (1.51) provides a way of constructing objects that is very dangerous for my users and that I'd like to prevent. Let's say I have my own integer class and I want to pass an optional such integer and store it in some class:
class…

Gurg Hackpof
- 1,304
- 1
- 13
- 26
8
votes
2 answers
Implementing boost::optional in c++11
I am experimenting with implementing boost::optional like data structure using c++11 features. Here is what I have so far :
template
struct maybe {
bool valid;
union {
T value;
};
maybe() : valid(false) {}
maybe(const T&…

keveman
- 8,427
- 1
- 38
- 46
8
votes
3 answers
boost::optional not letting me reassign const value types
It seems to me there should be four variants of boost::optional
optional => holds a mutable Foo and can be reassigned after initialization
optional const => holds a const Foo and can't be reassigned after…

HostileFork says dont trust SE
- 32,904
- 11
- 98
- 167
7
votes
1 answer
Is '_HAS_CXX17' macro usable in custom project headers to enable C++17 language set features?
I want to create headers which use 'optional' from standard C++.
However, My headers will be referred from Visual Studio 2015 as well as Visual Studio 2017 projects.
I would like to have something, such that for Visual Studio 2017 ( with C++ 17 lang…

Ishita
- 489
- 5
- 15
7
votes
1 answer
Temporary optional in the range-based for loop expression
Assume we have a function which returns std::optional. Then what is a proper way of using the result in the range-based for loop? The easiest way does not work:
for (auto&& e : a().value()) {
// ^--- A&& is returned, so A is…

Dmitry Panteleev
- 191
- 7
7
votes
1 answer
wrap boost::optional using boost::python
Is there a way to wrap boost::optional type object to expose it via boost::python::class_ (used from BOOST_PYTHON_MODULE)
struct Foo
{
boost::optional bar;
};
BOOST_PYTHON_MODULE(module_name)
{
class_("Foo")
…

Alexandra B.
- 263
- 2
- 10
7
votes
1 answer
failed attempt of using boost::optional
I have been trying to use boost optional for a function that could either return an object or a null and I cant figure it out. Here is what I have so far. Any suggestions on how to resolve this issue would be appreciated.
class Myclass
{
public:
…

MistyD
- 16,373
- 40
- 138
- 240
6
votes
0 answers
boost::optional strange uninitialize issue
Time to time gcc gives warning about work with boost::optional via maybe-uninitialized , but I have supposed that this is false positive (like described here https://github.com/boostorg/optional/issues/72).
But now valgrind reports the same issue…

user1244932
- 7,352
- 5
- 46
- 103
6
votes
1 answer
Is it safe to use boost::optional in interprocess memory?
Please consider following struct:
struct ThingThatWillGoInSharedMemory {
boost::optional opt_value;
};
I'm using boost::interprocess to create the shared memory area. My understanding of boost::optional was that it was a discriminated…

Benedict
- 2,771
- 20
- 21