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
1
vote
2 answers

Compile error while subclassing std::optional

I'm trying to subclass std::optional in MS C++17 (VS2017) to add a message field to the class, but getting the compile error error C2280: 'OptMsg::OptMsg(const OptMsg &)': attempting to reference a deleted function Intellisense gives…
buttonsrtoys
  • 2,359
  • 3
  • 32
  • 52
1
vote
4 answers

std::optional: Effective difference between simple and ref-qualified value()

This is an academic question. The std::optional type has a T && value() && method. I have the following definitions: class A { ... }; void f(A &&a); And the following program: std::optional optA; optA = A(1337); f(std::move(optA).value()); //…
Notinlist
  • 16,144
  • 10
  • 57
  • 99
1
vote
0 answers

I can't find where std::option comes useful

I found above code in this link and it tells that this example where std::option nicely fits! class UserRecord { public: UserRecord(const std::string& name, std::optional nick, std::optional age) : mName{ name },…
Islam Abdeen
  • 539
  • 3
  • 10
1
vote
2 answers

Can a function be applied to the value of a std::optional, getting back an optional?

I think that applying a function to an optional is a really useful pattern. It is however cumbersome to do with the C++ STL. For example: std::optional vec = tryCreateVector(); std::optional length = vec.has_value() ?…
decden
  • 679
  • 4
  • 19
0
votes
1 answer

Forward declaration of class inside unique_ptr inside optional with default argument fails

I have the following piece of code // DoSomething.h class Foo; void doSomething(std::optional> f = std::nullopt); If I include DoSomething.h anywhere without the definition of Foo the file doesn't compile, even if it…
Michael
  • 172
  • 12
0
votes
2 answers

optional refence to an object: best way?

I want a function to return an optional reference to an object. Idea is to avoid copy, but seems like i should not be using according to this discussion: std::optional specialization for reference types Lets say I have a map of objects, and I want…
mu5e
  • 61
  • 5
0
votes
1 answer

Is there any way to guard against using an unchecked std::optional?

For my purposes, I need to be able to say that my access to the .value of an optional is guaranteed to be valid. In the past, I have used a macro that checked against the .has_value() and called return T to jump out of execution, and then…
Richard Fabian
  • 697
  • 1
  • 5
  • 18
0
votes
0 answers

error: use of deleted function operator=(&&) with std::optional

I got myself an error sprouting from std::optional. Now when trying to reconstruct it, there seems to be something going on with defaulted ctors that I don't yet understand. Consider the following abstracted example: I'm assigning a config struct…
0
votes
1 answer

c++ make_optional for private constructor which takes unique_ptr as parameter

I followed this article to implement a class which has a private constructor and a static "make instance" function. The function returns a std::optional object depending on some condition, and will call the constructor only if the condition is met,…
user8822312
  • 255
  • 4
  • 14
0
votes
1 answer

Uninitialized memory wrapper

I'm looking for a class that is similar to std::optional, but without the internal flag which tells whether the container is empty or not. I want to be able to declare a variable of type T without invoking T' constructor, and later on move or…
ciamej
  • 6,918
  • 2
  • 29
  • 39
0
votes
2 answers

creating std::functional object to wrap a lambda with a call to static member function

EDIT Code I was talkin about here is available on my Github. https://github.com/SP8EBC/ParaGADACZ/blob/master/src/PlaylistAssembler.cpp . Lines between 103 and 110 I try to make a wrapper around value_of method from std::optional in c++17. An idea…
Mateusz L
  • 41
  • 1
  • 7
0
votes
1 answer

Does std::make_tuple() not work with objects of type std::optional?

This is the source code: using namespace std; class obj { public: obj() = default; obj(int i) : i_{i} {} int I() const {return i_;} int const & rI() const {return i_;} void I(int i) …
Ahmed R
  • 13
  • 3
0
votes
0 answers

std::optional compile passes, but run fails

I've been trying to introduce std::optional into my projects lately, and it's great! But now I'm a bit confused, look at the following code: #include #include #include using namespace std; int main() { …
xm lian
  • 73
  • 13
0
votes
1 answer

how do you destroy the value of a trivially destructible type within a disengaged trivially destructible sum type

A sum type such as std::optional is implemented with conditionally trivial member functions because the template parameter may have a non trivial destructor. It's destructor is thus implemented as follows: ~optional() { if (bool(*this)) { …
0
votes
0 answers

std::optional or alternatives for returning an argument by reference

I have the following function struct AlphaStore { unsigned int alpha1; unsigned int alpha2; } struct Alpha { int a=1; int b=2; }; AlphaStore* store; const Alpha& myFunction1(int x) { return reinterpret_cast