stdoptional Questions
3
Let's say I am writing a class that passes a std::variant<A,B> to another class.
While I design the class, I decide I'd like to keep a copy of the last thing that was passed to that other cla...
Audwen asked 5/11 at 14:31
7
Further in my code, I check to see check if an object is null/empty.
Is there a way to set an object to null?
Chandigarh asked 13/6, 2010 at 2:12
4
Is it possible to evaluate std::optional::value_or(expr) argument in a lazy way, so the expr were calculated only in the case of having no value?
If not, what would be a proper replacement?
Hydrated asked 5/8, 2018 at 15:12
3
Solved
I know that std::optional<T&> isn't supported in the standard. This question is about whether passing std::optional<T>& has any performance advantage
Sample code (https://godbol...
Mahican asked 22/9, 2021 at 2:59
1
Solved
Consider the following code:
#include <memory>
#include <optional>
template <typename T>
constexpr T * arg_to_pointer(T & arg) noexcept {
return std::addressof(arg);
}
int ...
Peggypegma asked 24/8, 2023 at 14:36
3
On cppreference, we can see std::optional takes a default value of U&& rather than T&&.
It makes me incapable of writing the following code:
std::optional<std::pair<int, int&g...
Orlina asked 24/5, 2023 at 12:50
2
Solved
I was having trouble when using foo.value_or(bar()) in my code, because I wasn't expecting the function bar() to be called when the optional variable foo had a value. I've since found this question...
Jelly asked 17/6, 2022 at 4:1
1
Why is the following snippet legal C++ code? This is a purely theoretical question - there is no usecase that I have in mind:
#include <optional>
#include <vector>
#include <io...
Jarret asked 10/5, 2023 at 10:22
1
Solved
I would like to ask how to return an std::optional in an efficient way and I would like to use std::make_optional().
For example lets have this code snippet:
std::optional<Path> CreateCanonic...
Marquismarquisate asked 20/1, 2023 at 9:18
3
Solved
I want to store a non-trivial type that is immovable and not copyable in an std::optional. However the object is constructed by a free function. (example)
struct Foo {
Foo();
Foo(Foo const&) ...
Opheliaophelie asked 10/1, 2023 at 9:23
3
Solved
C++23 adds some "monadic-style" functionality regarding optionals, as methods of optional<T>:
optional<T>::and_then() (and ignoring qualifiers of this):
template<class F&g...
Exigible asked 26/11, 2022 at 9:30
3
Solved
I looked several online std::optional documentary over the internet. However I could not be able to find any direct comparison between two cases below:
case 1:
SomePointer* foo::get_some_pointer(co...
Smash asked 4/11, 2021 at 11:32
3
Solved
I was reading References, simply and got to the part talking about using optional references. One of the reasons Herb gives to avoid optional<T&> is because those situations can be "...
Burglar asked 26/9, 2022 at 15:50
2
Solved
Things like the following happen all to often when using std::optional:
void bar(const Foo*);
void baz(const std::optional<Foo>& foo) {
// This is clunky to do every time.
bar(foo.has_...
Trehalose asked 22/9, 2022 at 15:13
3
Solved
C++23 std::optional is finally getting some very useful additions.
Since my knowledge of FP is very primitive I am wondering what is the syntax for the following two operations(that according to my...
Towny asked 6/1, 2022 at 11:6
0
While benchmarking code involving std::optional<double>, I noticed that the code MSVC generates runs at roughly half the speed compared to the one produced by clang or gcc. After spending som...
Centaur asked 25/6, 2022 at 16:9
6
Solved
How to avoid nested if statements with chained optionals in C++?
For example, if type A contains an std::optional<B> b and type B an std::optional<C> c, I would like to be able to writ...
Evocative asked 21/2, 2018 at 12:7
1
Solved
Is there an effective difference between std::move(*optional) and *std::move(optional)? Which one is preferable?
Full example:
#include <optional>
#include <vector>
void foo()
{
std::...
Bathhouse asked 2/5, 2022 at 15:8
2
Solved
How do you actually take a value out of optional? Meaning take ownership of the value inside the std::optional and replace it with std::nullopt (or swap it with another value)?
In Rust for example ...
Sonorant asked 23/4, 2022 at 13:20
1
I am playing with C++23 std::optional additions, but I can not figure out how to elegantly access the value of object if optional is active.
I know I can use if, but that is so C++20.
I really like...
Bronwyn asked 7/2, 2022 at 21:58
5
Solved
I have multiple functions that return a std::optional<T>. Here's an example for a made-up type MyType:
struct MyType {
// ...
}
std::optional<MyType> calculateOptional() {
// ... le...
Spicate asked 18/3, 2020 at 19:11
6
Solved
Why std::optional (std::experimental::optional in libc++ at the moment) does not have specialization for reference types (compared with boost::optional)?
I think it would be very useful option.
Is ...
Odontoblast asked 11/11, 2014 at 5:18
1
How can I return a value, similar to null, with std::optional?
My function receives an int index as parameter to iterate through a list, if the index value is not valid, how this std::optional can ...
Crumby asked 17/8, 2021 at 18:24
3
Solved
I'm writing a small C++11 library in which I believe std::optional would be a nice addition in some functions which can return nullptr. However, std::optional is a C++17 feature. Since being C++11 ...
Hoitytoity asked 21/8, 2019 at 19:40
2
Solved
I suppose I'm a bit confused as to how exactly optional values are stored. When constructing a class or struct that contains std::optional<T> members, will these members be stored contiguousl...
Clintclintock asked 10/6, 2021 at 14:18
1 Next >
© 2022 - 2024 — McMap. All rights reserved.