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
0
votes
1 answer
Different versions of destructor called depending on strange factors
Why do the following code generate such output?
main.cpp ctor 0x24a4c30
test.cpp dtor 0x24a4c30
test.cpp
#include
struct Test
{
Test()
{
printf("test.cpp ctor %p\n", (void *) this);
}
~Test()
{
…

wannabe programmer
- 73
- 6
0
votes
5 answers
Need help for C++ boost::optional
#include
#include
using namespace std;
boost::optional test_func(int i)
{
if(i)
return boost::optional(1234);
else
return boost::optional();
return (i); …

Arun Dambal
- 1
- 1
- 2
0
votes
1 answer
Initialization of an optional constant reference in a class
I have two classes, A and B. Class A is a transformation (a matrix) that performs a transformation on a given vector.
class A{
public:
...
A(...){};
...
void func_A(std::vector& vec){
/* Transform vector vec */
…

Pafnuty
- 1
0
votes
2 answers
How can I return char array with boost::optional
I try to return simple array with boost::optional
boost::optional foo () {
char ar[100] = {};
return boost::make_optional(true, ar);
}
and I got the following error:
could not convert ‘boost::make_optional(bool, const T&)…

Yuriy Gyerts
- 1,464
- 18
- 30
0
votes
1 answer
Optional passing object to function by reference
I have many calls to a function that takes just one argument and I don't want update those calls. But I want to call that function from some other special place but in that case it should additionally fill a vector that I will pass with some…

user7242858
- 81
- 1
- 9
0
votes
1 answer
Need clarification on boost::optional type
I’m trying to make sense out of a few details obtained from the core file with respect to boost::optional type variable.
Variable:
boost::optional cacher_;
Frame #5 from the core:
(gdb) p this->cacher_
$1 = boost::optional
The line being…

Maddy
- 1,319
- 3
- 22
- 37
0
votes
2 answers
C++ design pattern for an item with multiple representations
I have an "item" whose data can be represented 3 ways. I can convert from one representation to any of the others, at a runtime cost. Once I do the conversion I'd like to keep the new representation around. Any representation of the data can be…

user3375624
- 43
- 1
- 6
0
votes
2 answers
Can an uninitialised std::optional or boost::optional constructor throw?
Can either of the following template methods be declared noexcept?
template
std::optional foo(const T& value) // noexcept?
{
try {
// code possibly returning a T or a std::nullopt
} catch(...) {
return…

Daniel
- 8,179
- 6
- 31
- 56
0
votes
1 answer
Benefit from using boost::optional in the following usecase?
This is a very fundamental question. Is there any benefit in using boost::optional in the following scenario:
int somefunction(boost::optional value = getDefaultParam()){
return value->dosomething();
}
or
int…

SkypeMeSM
- 3,197
- 8
- 43
- 61
0
votes
1 answer
How to implement std::optional's copy constructor?
I am implementing std::optional, but have run into a snag with one of its copy constructors.
Here is a sketch of my implementation:
#include
template
class optional
{
public:
constexpr optional()
:…

Jared Hoberock
- 11,118
- 3
- 40
- 76