Skip to main content

Questions tagged [stdoptional]

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

stdoptional
-4 votes
0 answers
75 views

std::optional is not available though I'm using G++9 [closed]

`error: ‘optional’ in namespace ‘std’ does not name a template type ** | std::optional<double> ; | ^~~~~~~~ ‘std::optional’ is only available from C++17 onwards ** | std::...
o_o's user avatar
  • 1
-3 votes
2 answers
243 views

Why does `emplace()` without arguments return 0 and can we use it to read a `std::optional<unsigned>` variable? [duplicate]

Is there a way to typecast an optional unsigned std::optional<unsigned> variable to unsigned? #include <optional> #include <iostream> int main() { std::optional<unsigned> ...
thirdeye's user avatar
  • 306
3 votes
1 answer
74 views

Is it ok to double-dereference ("**itOpt") an optional iterator?

If I have a std::optional wrapped around (say) an std::vector::const_iterator, is it safe to access the referenced element with two consecutive dereference (*) operators? For example: typedef std::...
Scott McPeak's user avatar
  • 11.1k
5 votes
1 answer
412 views

`std::optional` factory function with guaranteed copy elision and `private` constructor, without passkey idiom

Consider the following pattern: class Widget { private: explicit Widget(Dependency&&); public: static std::optional<Widget> make(std::filesystem::path p) { std::...
Vittorio Romeo's user avatar
3 votes
3 answers
152 views

Why is it undefined behavior to call operator* on an empty std::optional if I don't access the returned value right away?

Why is it undefined behavior to call operator* on an empty std::optional if I don't access the returned value before emplacing an object? For example, why is the following code UB? std::optional<...
Adel M.'s user avatar
  • 388
0 votes
1 answer
104 views

optional vector of enum initializer list

In the following case, when using assignment on an optional vector of enum values I cannot explain the behavior of the code. Why does assignment of ... = {C} compile and create a vector of size 2, ...
Aedoro's user avatar
  • 856
0 votes
0 answers
56 views

Does optional<reference_wrapper> not return a reference?

I wanted to make a Table class that is supposed to work like a table with columns and rows, and values in the cells. Obviously I need a function that would allow to get the value from a particular ...
Marcin Pagórek's user avatar
0 votes
0 answers
93 views

Convert to std::optional in nlohmann::json via NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE

I need to serialize / deserialize a struct that has an optional member. I'm using the excellent nlohmann::json library and from the docs I can see that it should be done like this: namespace nlohmann {...
SupAl's user avatar
  • 949
0 votes
1 answer
99 views

Is it useful to construct an std::optional using std::in_place when returning from a function?

I was just wondering, is it useful to return an std::optional from a function constructed using std::in_place? Should I be doing it blindly or does copy elision/changing C++ versions/the data ...
Alexandre Deus's user avatar
1 vote
1 answer
76 views

Unexpected behavior of std::is_copy_assignable and boost::optional

If I delete the copy constructor and/or the copy-assignment constructor of a type Bar, struct Bar { Bar() = default; Bar(Bar const&) = delete; }; std::optional<Bar> is not copy-...
joergbrech's user avatar
  • 2,742
-4 votes
1 answer
168 views

error: cannot convert 'std::optional<int>' to 'const int' in initialization

#include <vector> #include <ranges> #include <algorithm> #include <functional> using namespace std; int main() { const vector<int> v = {1, 2, 3}; const int n = ...
Ludovic Aubert's user avatar
3 votes
0 answers
198 views

Runtime Issues with std::optional in GCC on M2 Mac

I have found what I believe to be a weird bug with std::optional in GCC on my macbook. I am hoping that someone can either show me that I'm wrong, or offer guidance about where to report it. ...
arobinson's user avatar
  • 166
0 votes
2 answers
66 views

Writing functions which handle invalid inputs in C++

In C++, how to handle function outputs , if the inputs are invalid. Say, I am writing a function to multiply two matrices. How to deal with it if the two matrices cannot be multiplied (due to in-...
Vedant Yadav's user avatar
4 votes
3 answers
174 views

Pass-through constructor for std::optional arguments

Alright, I messed up asking this question the first time. Is there a way, idiomatically, to provide a constructor which takes one or more std::optional<T> and returns a std::optional<U>? ...
kc9jud's user avatar
  • 338
2 votes
1 answer
154 views

Pass-through constructor for std::optional argument

Is there a way, idiomatically, to provide a constructor/conversion which takes a std::optional<T> and returns a std::optional<U>? For instance, ideally I would love some kind of syntax ...
kc9jud's user avatar
  • 338

15 30 50 per page
1
2 3 4 5
14