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

C++ Compile error:'HelpModel' was not declared in this scope - using an optional

I'm getting the following error when compiling, using GCC 11.2.0: /src/SearchController.h:12:23: error: ‘HelpModel’ was not declared in this scope 12 | std::optional searchModel; | …
Mr Eeeeee
  • 1
  • 1
0
votes
1 answer

How to sort container with std::optional type?

Looking for a way to sort an optional container Like the following... #include #include using optional = std::optional; std::vector cont; int main() { auto min_iter = std::min_element(cont.begin(),…
WillisM
  • 121
  • 1
  • 5
0
votes
1 answer

Using std::nullopt as a default value for an std::optional argument in C++17

This is a standard / good coding practice question. I've recently started using std::optional in my codebase. I think it's great (and verbose) for specifying an argument that's optional, so long as a reference doesn't need to be passed to that…
heothesennoc
  • 541
  • 5
  • 10
0
votes
1 answer

Is there any safe way to derive the offset of a T inside a std::optional?

Given a std::optional is it possible to derive the address of the std::optional from the address of the T? possible implementation: template struct myoptional { static std::size_t getOffset(void) { static const…
Frank Puck
  • 467
  • 1
  • 11
0
votes
0 answers

Unknown error when returning std::optional with vectoru in big sets

So I have two simple classes, one cleaning unwanted values from vector and second one calling the first one. At the end I'm getting value from std::optional. That's it. It works fine with small sets but if I call test with vector.size() around 300…
Gameriker
  • 35
  • 7
0
votes
0 answers

clang++ does not find my templated function definition

I want to support ostream for std::optional. My problem is std::optional is captured by the new function that I have added, but std::optional is not captured. Inside foo.h, I have the following code template
user3219492
  • 774
  • 1
  • 10
  • 18
0
votes
1 answer

How to construct a std::optional of a struct with std::shared_ptr

I'm trying to create an optional with a class that has a shared_ptr but the implicitly deleted constructor prevents me from using make_optional to create it. Is the only option to manually write that constructor? Here is a simple…
cjds
  • 8,268
  • 10
  • 49
  • 84
0
votes
3 answers

Accessing std::vector methods inside std::optional

I have got an Element, which is a std::vector in a std::optional. Now I want to emplace an Element to the vector (via emplace_back): #include "stdafx.h" #include #include #include int main() { …
Gabriel
  • 11
  • 5
0
votes
0 answers

std::optional:: possible to ignore folloe up appearence of a variable?

I am beginner to C++, so my question is probably very naive. For instance, during runtime I decide to initialize a variable using std::optional, i.e. the variable is only defined under certain conditions. My idea is now: Ignore all the following…
Simon
  • 325
  • 1
  • 6
0
votes
1 answer

motivation behind in_place_t in std::optional constructor

I'm trying to understand the motivation behind in_place_t in the std::optional constructors a little better. I get the use case to disambiguate construction of null optional vs. type T with default constructor. However if there are one or more…
user3882729
  • 1,339
  • 8
  • 11
0
votes
0 answers

Using std::optional template in another optional Elemet of a branched datastructure

I have got a branched datstructure, which is mainly build out of structs and Integers. Some Elements are signed with the std::optional template. In the case of an optional struct element, in another struct, which itself is also referenced in another…
Gabriel
  • 11
  • 5
0
votes
1 answer

How to use std::optional for error handling when creating objects without instantly destructing them?

Error handling is a challenge in C++ constructors. There are several common approaches but all of them has obvious disadvantages. Throwing exceptions for example, may cause leak of the allocated resources earlier in the constructor, making it an…
Z E Nir
  • 332
  • 1
  • 2
  • 15
0
votes
0 answers

Construct std::optional from std::option

This code gets rejected: const std::optional opt{5}; std::optional opt2 = opt; But this is accepted: const std::optional opt{5}; std::optional opt2 = opt; What is going on here? In file included from…
Artefacto
  • 96,375
  • 17
  • 202
  • 225
0
votes
2 answers

g++ skipping a function when compiling to object file

the solution to this is probably trivial, but I can't find it. I tried to google it but with no luck. I'm working on a C++ project using g++ on linux (gcc version 10.1.0 Ubuntu 10.1.0-2ubuntu1-18.04). g++ compiles a C++ file into an object .o…
Duccio
  • 143
  • 2
  • 7
0
votes
1 answer

How to upcast an static std::optional

class Child: public Base {.....} class Child { static std::optional& Child::GetBaseInstance() { static std::optional instance = std::make_optional; \\Does not work. Please help return instance; } } I'm…
rstr1112
  • 308
  • 4
  • 13