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
198
questions
-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::...
-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> ...
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::...
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::...
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<...
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, ...
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 ...
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 {...
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 ...
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-...
-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 = ...
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.
...
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-...
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>? ...
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 ...