All Questions
Tagged with option-type c++
176
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::...
-4
votes
2
answers
250
views
Why `emplace()` without arguments returns 0 when I try to read an `std::optional<unsigned>` variable? [duplicate]
Below is the example code snippet:
#include <optional>
#include <iostream>
int main()
{
std::optional<unsigned> check = 200;
std::cout << check << std::endl;
...
1
vote
8
answers
138
views
Decent idiom for initializing an optional to either null or a value?
Motivation
With C++ having gotten optional's (in C++17), it is now common to want to write the equivalent of:
If a condition holds, initialize my variable with some expression; and if the condition ...
1
vote
0
answers
59
views
Pure functional way of creating a std::optional counterpart
I'm working on a pure functional library for c++ and I met a design problem. I was creating the monad Option. This is my implementation
namespace ns {
struct _NoValue_t {};
const _NoValue_t ...
0
votes
2
answers
82
views
Is using a wrapper with a 'check' function a good approach to optional types rather than the traditional method? [closed]
languages like C++ and Rust have an Option type, which is basically an enum/bool with a value.
But I feel like this can have a few problems like:
the extra overhead of returning/passing an extra ...
2
votes
1
answer
207
views
Why does calling a virtual class's virtual method inside std::optional generate a call to the base method, not an overridden?
Suppose we have some virtual class Callable, (not abstract, because we cannot use abstract classes as type of std::optional<T>), and some function (call_option) that takes an std::optional of ...
0
votes
0
answers
53
views
C++ enums with additional data (similarly to ML/Haskell datatypes)
I would like to create a function that returns an enumeration value, but just in case of specific values, it also provides some additional data. In SML I would define the return type as
datatype ...
7
votes
2
answers
3k
views
Why is there no built-in way to get a pointer from an std::optional?
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....
0
votes
3
answers
402
views
How do I pass and modify an optional argument to a function through a header file in C++?
I am trying to create a function with an optional argument, where the optional argument gets modified/initialized in the function. This is part of a much larger program so I am using a header file. I ...
1
vote
1
answer
188
views
Does C++ have some equivalent of SQL coalesce?
In SQL, COALESCE(val_1, val_2, ... val_n) is a variadic function which returns its first non-null argument, or null otherwise.
Now, in C++, we have pointers which can be null, but also std::optional's ...
1
vote
0
answers
36
views
C++ optional initialization issue with Clang? [duplicate]
Following code compiles fine in C++20 with MSVC and GCC, but fails with clang.
#include <optional>
#include <string>
using namespace std;
struct A {
string s;
};
int main() {
[[...
3
votes
3
answers
795
views
Iterating over std::optional
I tried to iterate over an std::optional:
for (auto x : optionalValue)
{
...
}
With the expectation of it doing nothing if optionalValue is empty but doing one iteration if there is a value ...
2
votes
1
answer
362
views
GCC problem with std::optional and packed struct member
I need to use a packed struct for parsing incoming data. I also have an std::optional value that I want to assign the value of one of the struct members. However, it fails. I think I understand the ...
4
votes
1
answer
971
views
Does std::optional<>::emplace() invalidate references to the inner value?
Consider the following fragment (assume that T is trivially constructible and trivially destructible):
std::optional<T> opt;
opt.emplace();
T& ref = opt.value();
opt.emplace();
// is ref ...
16
votes
2
answers
15k
views
Take value out of std::optional
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 you ...