Skip to main content
Best practices
0 votes
8 replies
195 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 ...
sam's user avatar
  • 911
Best practices
0 votes
7 replies
177 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 ...
Caduchon's user avatar
  • 5,278
2 votes
2 answers
304 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 ...
Raildex's user avatar
  • 5,428
-2 votes
2 answers
230 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 ...
user149408's user avatar
  • 6,279
1 vote
1 answer
133 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::...
einpoklum's user avatar
  • 137k
15 votes
1 answer
746 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 ...
MarkB's user avatar
  • 2,230
5 votes
1 answer
177 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 ...
Stephen's user avatar
  • 735
3 votes
0 answers
96 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 ...
xaxazak's user avatar
  • 998
3 votes
3 answers
201 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, ...
bramar2's user avatar
  • 79
1 vote
1 answer
144 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 ...
Niccolò Tiezzi's user avatar
2 votes
2 answers
140 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) {} //...
Konstante's user avatar
  • 671
0 votes
2 answers
205 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::...
H.v.M.'s user avatar
  • 1,746
3 votes
2 answers
137 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 ...
Igor R.'s user avatar
  • 15.1k
2 votes
1 answer
147 views

If I have an std::optional<T> then how can I transparently cast it to a std::optional<U> given that T and U are compatible types and preserving std::nullopt? That is, if the initial std::...
Kamajii's user avatar
  • 1,898
1 vote
1 answer
200 views

The following code compiles well for Visual Studio < 17.2: #include <optional> #include <map> class __declspec(dllexport) A { using M = std::map<int, A>; std::optional<...
αλεχολυτ's user avatar

15 30 50 per page
1
2 3 4 5
15