Questions tagged [stdoptional]

In C++ the std::optional class template represents an optional value, i.e. one that may or may not be present.

The C++ class template std::optional<T> represents an optional value of type T, so that either it contains a valid value of type T or it contains nothing.

Related tags

173 questions
0
votes
0 answers

Performance between std::vector and std::array, n> to store small amount of data?

I have a 2D Environment where I want to get for some cells the Moore neighborhood (east, west, north and east cells). Sometimes a cell can only have 3 or 2 neighbors (if we check from a border or a corner of the grid). Above this, I'd like to…
blaoi
  • 143
  • 4
0
votes
2 answers

Return optional from class method

I have a optional member in a class, which I want to return by value, via a method. Sample code: #include #include #include using namespace std; class bar { public: int a; bar(const bar &obj) { a =…
Nishant
  • 2,571
  • 1
  • 17
  • 29
0
votes
2 answers

error: no matching function for call to 'std::optional::optional(smoltcp::Buffer&)'

I am trying to make a function that returns an std::optional to Buffer: std::optional receive(SmolSocket smolSocket) { //... return std::optional(buffer); } Where Buffer is simply struct Buffer { public: …
Guerlando OCs
  • 1,886
  • 9
  • 61
  • 150
0
votes
0 answers

usage of std optional mixed with shared_ptr

I have two derived classes as follows. This is the simplified design version that I have right now. class A objects are copy-able but they are big. That is why I used reference in the constructor of the derived_1 class. I used shared_ptr for the…
H'H
  • 1,638
  • 1
  • 15
  • 39
0
votes
0 answers

std::optional call by reference

I am implementing the following function: bool DenStream::_try_merge(const double sample, const double weight, const std::optional& micro_cluster) const { if (micro_cluster) { …
A Hansson
  • 77
  • 2
  • 9
0
votes
1 answer

Is it safe to disable warning C4324 for std::optional?

I am using flatbuffers - a serialization library. In the scheme I define a struct that I want to use with std::optional. but during compilation I get a warning C4324. What I understand flatbuffers use custom padding for generated structures. Is it…
Jiří Lechner
  • 750
  • 6
  • 19
0
votes
1 answer

Can I safely get a pointer to uninitialized std::optional content to read into memory from a binary file without default constructing the contents?

I'm refactoring some legacy code which reads some binary data from a file into struct. It occurred to me that changing the variable to a std::optional could ensure the variable actually read (initialized) before it is used. But the file reading…
Dennis
  • 2,607
  • 3
  • 21
  • 28
0
votes
1 answer

std::optional taking non-copyable?

Suppose i have something akin to this pseudocode: std::optional > > getDataTogetherWithLock() { if (!some_ptr) { return std::nullopt; } return some_ptr->getDataTogetherWithLock();//just…
darune
  • 10,480
  • 2
  • 24
  • 62
0
votes
3 answers

Get result into the caller from a function returning std::optional of std::vector

I am trying to manipulate the std::optional using container like std::vector. I started by doing the code below : #include #include #include #include using namespace std; using optional_vecs =…
Blood-HaZaRd
  • 2,049
  • 2
  • 20
  • 43
0
votes
0 answers

Variable pointing on empty space while passing aguments using std::move from std::optional buffer

Trying to write simple code to simulate parsing packages in the factory. I am sending a package which is saved in the buffer like: sending class: class PackageSender { public: void send_package(); std::optional
0
votes
2 answers

Is it safe to handle a std::optional in the body of a noexcept function?

I wanna use std::optional as a Maybe type and I'm concerned wether I can use it to indicate a potential failure in some computation, the so observed as an empty option. For example, consider: // This function doesn't have state std::optional
Nazinho
  • 311
  • 2
  • 8
0
votes
0 answers

Optimize the codes which finds an element from a vector

I have the following code, which tries to find an element from myPairs, which is a std::vector of MyPair. The code works fine, but I feel it could be more elegant. Can anyone please advice, thanks. std::optional find_right_data(const MyPairs…
Edamame
  • 23,718
  • 73
  • 186
  • 320
0
votes
1 answer

C++: How to initialize and call a method on std::optional?

Let's consider this code std::optional> object; void f(int v) { if (!object.has_value()) { object = flat_set{}; } object->insert(v); } How can I avoid if part? Is there any way to write the body of function…
Tigran Sahakyan
  • 363
  • 1
  • 5
  • 10
0
votes
1 answer

C++17 std::optional error: expected primary-expression before 'auto'

I was experimenting with C++17 feature std::optional The optional return type is std::optional>. I call the sum_pair function in print_answer function and wanted a optional print. In print_answer function I wanted to check…
Const
  • 1,306
  • 1
  • 10
  • 26
0
votes
0 answers

clang-4.0 and std::optional support

According to clang's C++-1z status page, I believe 4.0 should support C++17 std::optional. However I'm having trouble getting it to work. See this simple example: #include int main() { return 0; } Trying to compile it in various…
Ela782
  • 5,041
  • 5
  • 53
  • 66
1 2 3
11
12