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
1
vote
2 answers
passing boost::optional lvalue as a reference to a function
Can you somehow pass boost::optional lvalue as a reference into a function which changes the value? Something like this (http://coliru.stacked-crooked.com/a/f77d3b095af3d66b):
#include
#include
void foo(int& x)
{
…

clime
- 8,695
- 10
- 61
- 82
1
vote
3 answers
boost::optional vector pass by reference as default parameter
boost::optional> filePath;
If I have the above boost optional vector is it possible to then pass this by reference and as an optional parameter?
Test(const boost::filesystem::path& targetPath,…

CodersSC
- 710
- 2
- 13
- 29
1
vote
2 answers
Equivalent behaviour for template template arguments to function templates
I am trying to write code like this:
template class C>
boost::optional search(const C& dict,
const K& key)
{
auto it = dict.find(key);
if (it !=…

CppNoob
- 2,322
- 1
- 24
- 35
1
vote
2 answers
why not use boost::optional as a better scoped_ptr
why isn't it common place to use boost::optional as a scoped_ptr, it seems like it is better as the object is created on the stack instead of the heap. But I have never seen it used this way. My question is, what are the disadvantages of using a…

dan
- 1,525
- 1
- 17
- 35
1
vote
2 answers
C++ optional<> and delayed construction of noncopyable object
See the below code,
The question is: how can I delay the construction of an object that is non-copyable, using optional<>.
I'm using boost::optional in the example, although I believe its now in the std::optional standard too.
Yes, I could use…

elegant dice
- 1,307
- 12
- 19
0
votes
1 answer
boost::serialization of boost::optional of type with private default constructor
I'm upgrading from boost 1.54 to the latest 1.80 and have a compilation problem with boost serialization.
I have a class A with private default constructor. Another class B has a boost::optional field and also is boost::serializable.
To allow…

Alex Che
- 6,659
- 4
- 44
- 53
0
votes
1 answer
Use std::function to wrap a function with optional arguments (using maybe boost::optional), and serve as a template in another class
In my recent project I want to define a class X which has an input functional in its constructor, i.e., std::function. In real applications, the argument of class B is optional (sometimes it will not have this argument).…

lucasyu
- 5
- 2
0
votes
1 answer
boost::make_optional with type specification
I'm puzzled about the boost::make_optional() behavior when used with the template specification.
In particular, it's still unclear to me why this:
int pizza = 5;
boost::optional pizza_opt = boost::make_optional(pizza)
throws the compile…

giubacchio
- 117
- 1
- 12
0
votes
1 answer
std::pair() having empty values with something like boost::optional
class LinkStats{
public:
LinkStats(std::shared_ptr config) {
m_interface = "eth0";
};
~LinkStats() = default;
std::pair getRxLinkStats() {
auto status = getLinkStats();
…

liv2hak
- 14,472
- 53
- 157
- 270
0
votes
0 answers
c++ - is it possible to create an optional promise?
consider the following code:
#include
#include
struct S {
std::promise p;
};
void f() {
boost::optional o = std::move(S());
}
it fails to compile because somewhere deep inside boost::optional…

user2717954
- 1,822
- 2
- 17
- 28
0
votes
1 answer
Overload ambiguity with boost::optional, workaround?
I would like to overload with boost::optional but cannot due to overload ambiguity and am looking for a workaround.
For example, the following code will not compile due to ambiguity:
void foo() { ... }
void foo(const Class& A) { ... }
//…

gchen
- 126
- 6
0
votes
1 answer
boost::optional - using boost::in_place for the object which constuctor takes other object by the reference
I try to use boost::in_place for the non-movable and non-copyable object which constructor takes other object by the reference:
struct A
{
};
struct B
{
B(A& a): a_(a){}
B(B const &) = delete;
B(B&&) = delete;
B& operator=(B const…

Irbis
- 1,432
- 1
- 13
- 39
0
votes
1 answer
How to initialize non-movable and non-copyable class member - boost::optional
I have a non-movable and non-copyable type:
struct A
{
A(std::string p1, int p2){}
A(A const &) = delete;
A(A&&) = delete;
A& operator=(A const &) = delete;
A& operator=(A&) = delete;
};
I can construct boost optional this…

Irbis
- 1,432
- 1
- 13
- 39
0
votes
1 answer
Using std::experimental::optional giving compilation errors
I am using the optional feature for the 1st time, any idea what is wrong in this code I wish to retrieve the value from the tuple which is stored in retCode
#include
#include…

Vinay Shukla
- 1,818
- 13
- 41
0
votes
0 answers
Different behaviour if type is a reference
I'm using the function tryValue very often in my projects. It is simple wrapper around boost::any, that simplifies greatly its everyday usage.
#include
#include
#include
#include…

Aleph0
- 5,816
- 4
- 29
- 80