Questions tagged [boost-optional]

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

100 questions
3
votes
2 answers

Boost::optional dereference

I am reviewing some code and have something like this: boost::optional isSet = ...; ... some code goes here... bool smthelse = isSet ? *isSet : false; So my question is, is the last line equivalent to this: bool smthelse = isSet;
Eduard Rostomyan
  • 7,050
  • 2
  • 37
  • 76
3
votes
1 answer

Do optional references in c++ preserve object life?

Say I have: #include #include "boost/optional.hpp" struct cat { int paw = 4; }; int main() { boost::optional z; { cat realCat = cat(); z = realCat; } std::cout << z->paw << std::endl; return…
Carbon
  • 3,828
  • 3
  • 24
  • 51
3
votes
3 answers

C2143 syntax error when including boost/optional.hpp

I'm stuck with a compile-time error which I cannot understand. I try to use boost::optional in my code, and as soon as I include boost/optional.hpp I cannot build my project any longer. If I comment this include statement out, it works. I don't even…
3
votes
1 answer

Can I safely point to the data of a reassigned boost::optional?

Given the following code sample: boost::optional< int > opt; opt = 12; int* p( &*opt ); opt = 24; assert( p == &*opt ); Is there any guarantee that the assert will always be valid?
Julien
  • 2,139
  • 1
  • 19
  • 32
3
votes
3 answers

Viewing a raw pointer as a range in range-based for-loop

How can I make a raw pointer behave like a range, for a for-range loop syntax. double five = 5; double* dptr = &five; for(int& d : dptr) std::cout << d << std::endl;// will not execute if the pointer is null Motivation: It is now vox populi that an…
alfC
  • 14,261
  • 4
  • 67
  • 118
3
votes
1 answer

how to divide boost::optional?

I have such code: boost::optional result = _ind1.Value() / _ind2.Value(); Each arg is boost::optional too: boost::optional Value() { return value; } Errors are: Error 1 error C2676: binary '/' : 'boost::optional'…
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
3
votes
2 answers

How can I make sure a boost::optional object is initialized in release-build?

When trying to get the value of a boost::optional object, BOOST_ASSERT is used to make sure the object is indeed initialized. But what I would like when dereferencing an uninitialized optional is for an exception to be thrown - is there any way to…
Danra
  • 9,546
  • 5
  • 59
  • 117
2
votes
3 answers

How to decay rvalues reference type whilst preserving lvalue references in C++14?

I have the following C++14 lambda https://godbolt.org/z/aexxTY #include int main(){ auto foo = [](auto && t) { using T = decltype(t); return boost::optional(std::forward(t)); }; // This…
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
2
votes
1 answer

How to rewrite with boost::optional in C++11?

How do I rewrite the following code to use boost::optional or boost::none, in C++11? std::unique_ptr find( std::string key) { std::map(void)> > m{ {"key1",…
javauser
  • 31
  • 1
2
votes
1 answer

How to forward constructor arguments to boost::optional

I have the following simple struct: struct X { X(std::string name, int value): name_(name), value_(value){} std::string name_; int value_; }; I would like to use it with boost optional without copying. Here is one…
Irbis
  • 1,432
  • 1
  • 13
  • 39
2
votes
2 answers

Why does this use of boost::none fail to compile with nvcc?

I'm trying to compile the following code: #include void foo(boost::optional x = boost::none); placed in the file a.cu, with the CUDA compiler, using the following command line: nvcc a.cu -c --std=c++11…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
2
votes
0 answers

nlohmann json ambiguous overload for 'operator='

I'm getting this compilation error with the following code #include #include #include "nlohmann_json.hpp" namespace nlohmann { template struct adl_serializer> { static void…
SU3
  • 5,064
  • 3
  • 35
  • 66
2
votes
2 answers

Do std::optional and boost::optional respect alignment restrictions of the managed object?

If a class T has an alignment requirement, such as one specified by the alignas keyword, are std::optional and boost::optional guaranteed to respect said alignment? If they were simply wrapper classes for a T object and a bool initialized,…
patatahooligan
  • 3,111
  • 1
  • 18
  • 27
2
votes
1 answer

How to properly use boost::program_options::implicit_value for vector of string?

Proof of code: boost::program_options::options_description options; Parser::Parser(): options("Allowed options") { options.add_options() ("help,h", "produce help message") ("type,t", po::value()->required()->implicit_value(""),…
andrew
  • 3,083
  • 4
  • 24
  • 29
2
votes
1 answer

Boost Optional with Boost Thread compilation issue

My environment is Visual Stuido 2013, VC12, Boost 1.59. The following code (a minimal repro of the real code): #include "boost/thread.hpp" #include "boost/optional.hpp" class MyClass { public: template operator const T& ()…