close
Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
Filter by
Sorted by
Tagged with
Filter by Employee ID
Score of 12
1 answer
640 views

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

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

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

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, 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 ...
user avatar
Score of -2
2 answers
339 views

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

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

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

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

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

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

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

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 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

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 ...

15 30 50 per page
1
2 3 4 5
15