218 questions
Score of 12
1 answer
640 views
How to write a custom formatter for std::optional
I'm trying to write a custom std::fmt formatter for std::optional. Here's what I have so far:
#include <fmt/core.h>
#include <fmt/ranges.h>
#include <vector>
#include <optional>...
Advice
0
votes
10
replies
267
views
Valid but unspecified state. What is specified then?
The standard says that after a move, an object is in a "valid but unspecified state". I found nothing in the standard on the properties of being in a "specified state" and being in ...
Best practices
0
votes
8
replies
222
views
Should I use std::move when returning a local std::string as std::optional<std::string>?
My confusion is about the return statement inside readFileContents:
The function returns std::optional<std::string>, but the local variable is a std::string.
NRVO doesn’t apply here because ...
Best practices
0
votes
7
replies
239
views
One-line call to std::optional::emplace only if it has no value
In the same spirit than the creation of an instance in a std::map when calling operator[] , I would like to emplace (direclty in the expression) in an std::optional only if has_value() is false. In ...
Score of 2
2 answers
584 views
Is there a possibility that sizeof(std::optional<T>) == sizeof(T)?
Is there a possibility, where sizeof(T) is the same as sizeof(std::optional<T>)?
I can imagine that nullptr would signal a "disengaged" std::optional<T*> but even that might not ...
Score of -2
2 answers
339 views
Optional const by-reference argument in C++
In a C++ program, I have an API function with the following signature:
Foo const & GetFoo() const;
The Foo class doesn’t allow copying or moving of instances. The API can’t easily be changed.
Now ...
Score of 1
1 answer
152 views
I can't get an optional with (cond) ? value : nullopt ... what should I write instead?
Suppose I'm implementing the function returning an std::optional<T>. In that function, I compute some condition, and then want to return either a value, or a nullopt. I can do it like so:
std::...
Score of 15
1 answer
778 views
Is implicit conversion to std::optional guaranteed to use move constructor?
The following code block illustrates a difference between returning a std::optional via an implicit conversion (i.e. fn) vs. an explicit construction (i.e. fn2). Specifically, the implicit conversion ...
Score of 5
1 answer
200 views
range-based for-loop in C++ over std::optional<Container> does not work
Let me start with a C++ code that simplifies my issues I faced in the actual code base. I compiled it with --std=c++20 and --std=c++17. The first for-loop below was okay; the second for-loop, which ...
Score of 3
0 answers
103 views
inheriting "operator =" from std::optional<T> (as a parent class) fails when T is native type
I'm wrapping std::optional<T> to add a few IMHO useful functions of my own, and I noticed that my code kept failing when T is native.
When assigning a variable of my wrapped-optional type to a ...
Score of 3
3 answers
249 views
Correct way of inserting std::optional<T> into std::vector<T>
I have a class T, it looks like this:
class T {
uint64_t id;
std::string description;
char status;
uint64_t createdAt;
uint64_t updatedAt;
T(uint64_t c_id, std::string_view c_description, ...
Score of 1
1 answer
170 views
`std::shared_ptr` vs `std::optional` for thread safe queue
In "C++ Concurrency in action" from Anthony Williams (2012) there is a thread safe queue implemented by storing std::shared_ptr<T> like so
template<typename T>
class ...
Score of 2
2 answers
150 views
Return std::optional<T> where T's constructor is private
I still do not understand the behavior of std::optional in the following code:
class A
{
public:
// A(int x, int y) : x(x), y(y) {} // always compiles
private:
A(int x, int y) : x(x), y(y) {} //...
Score of 0
3 answers
265 views
Why can't I assign an std::optional of std::lock_guard?
Why can't I declare an optional std::lock_guard and then assign it later? The same thing with an optional string works just fine.
This works:
std::mutex m;
std::optional<std::lock_guard<std::...
Score of 3
2 answers
158 views
Function template call: type deduction and empty brace-enclosed initializer list
Consider the following function template calls:
#include <optional>
template <class T = int>
void f(std::optional<T>);
int main() {
f(1); // (1)
f({}); // (2)
}
The first ...