stdoptional Questions

1

Solved

In the std::optional::emplace docs there is an overload that accepts std::initializer_list: template< class U, class... Args > T& emplace( std::initializer_list<U> ilist, Args&&...
Luminal asked 9/6, 2021 at 14:57

3

It is very useful to be able to compare for equality a std::optional<T> with T: std::optional<int> opt_value; int value = 123; opt_value == value; // will always be 'false' I th...
Scribbler asked 29/4, 2021 at 15:55

0

In real code, constructors can be very complex and so the emplace of a std::optional can query the status of the optional itself. When this happens it's usually a bit more convoluted, but here is a...
Oringas asked 29/4, 2021 at 11:28

1

Solved

How to in-place construct an optional aggregate? It seems I can only construct an optional single thing, and not an optional aggregate of things. #include <optional> #include <iostream>...

3

Solved

note: this question was briefly marked as a duplicate of this, but it is not an exact duplicate since I am asking about std::optionals specifically. Still a good question to read if you care about ...
Seasoning asked 2/4, 2021 at 17:9

2

Solved

I am trying to write a function, make_foo, that will "unwrap" a std::optional< foo >, returning the contained value. The function assumes that the optional is engaged so does not pe...
Gildea asked 22/3, 2021 at 14:2

2

Solved

All the std::make_ are made redundant by C++17 with the introduction of Class template argument deduction (except make_unique and make_shared). So what is the point of std::make_optional? As far as...
Trinitrotoluene asked 4/7, 2020 at 22:1

3

Solved

The following code: std::optional<std::string> so; std::cout << so->size() << std::endl; std::cout << so.has_value(); outputs: 0 0 My question is whether its safe...
Wellhead asked 10/11, 2020 at 20:21

4

Solved

I have a method that returns an optional struct, like this: auto getBook(const std::string &title) const -> std::optional<Book>; I want to call this method in another method that retu...
Jaipur asked 23/10, 2020 at 12:44

2

Solved

I'm familiar, that the append in std::string returns std::string& and therefore it do not qualify to be moved from, so the result will not be moved. #include <string> int main() { std:...
Pythagoras asked 21/7, 2020 at 9:48

1

Solved

Cppreference has the following description of mixed (optional and some other non-optional type) comparison operators for std::optional: Compares opt with a value. The values are compared (using th...
Mariann asked 23/6, 2020 at 12:46

1

Solved

I am doing the Vulkan Tutorial https://vulkan-tutorial.com/ #define GLFW_INCLUE_VULKAN #include<GLFW/glfw3.h> #include<optional> struct s { std::optional<uint32_t> num;//Intelli...
Microscopy asked 14/6, 2020 at 8:15

4

How std::optional fits in my code? My code contains MANY functions that looks roughly like: bool GetName(_Out_ string& strRes) { // try to get/calculate the value // value may also be empty...
Whitehorse asked 20/6, 2019 at 6:40

3

Solved

std::variant can enter a state called "valueless by exception". As I understand, the common cause of this is if a move assignment throws an exception. The variant's old value isn't guaran...
Impaction asked 28/8, 2019 at 15:55

1

The standard mandates that the move assignment operator of optional ... constexpr optional& operator=( optional&& other ) [...] shall not participate in overload resolution unless...
Sello asked 18/3, 2019 at 20:49

2

std::optional can use the syntax to access its value similar to a normal pointer, like . std::optional<string> some_str; if (some_str) (*some_str).c_str(); but it also has two functions,...
Fania asked 10/5, 2019 at 17:50

1

I was wondering if this would be considered a valid usage of std::optional. I have a function that returns a process_id (std::uint32_t value), would it be more efficient to have a standard "std::ui...
Irs asked 29/4, 2019 at 6:35

1

I perfectly understand that because of performance reasons the operator* in std::optional does not make any checks for the actual existence of a contained value. However, in debug mode performance ...
Pompey asked 17/4, 2019 at 12:18

1

Solved

I was reviewing the interface for the C++-17 std::optional class template and noticed that the reset and assignment from nullopt are not marked as constexpr. Was this an oversight or is there a re...
Doggy asked 20/12, 2018 at 21:25

3

Solved

I am unclear if the correct code to pass an optional vector of ints to a function by reference is : void test_func(std::optional<std::vector<int>&> vec) or void test_func(std::o...
Whirly asked 9/12, 2018 at 2:45

1

Solved

I want to create headers which use 'optional' from standard C++. However, My headers will be referred from Visual Studio 2015 as well as Visual Studio 2017 projects. I would like to have something...
Antidromic asked 18/9, 2018 at 4:59

2

Solved

While reading through GCC's implementation of std::optional I noticed something interesting. I know boost::optional is implemented as follows: template <typename T> class optional { // ... ...
Gritty asked 14/9, 2018 at 19:34

1

Solved

Here's the definition of value_or() from the C++17 standard: template <class U> constexpr T value_or(U&& v) const&; Effects: Equivalent to: return bool(*this) ? **this : ...
Sigmoid asked 3/5, 2018 at 18:53

2

Solved

A really strange and unexpected behaviour of clang 5 was detected when switching to c++17 and replacing custom std::optional solution with the standard one. For some reason, emplace() was being dis...
Slouch asked 26/12, 2017 at 6:48

3

Solved

I'm porting code to C++17, trying to use the new features while possible. One thing I like is the idea to use std::optional to return or not a value in a function that may fail in some conditions. ...
Doucet asked 16/12, 2017 at 1:36

© 2022 - 2024 — McMap. All rights reserved.