216 questions
Best practices
0
votes
8
replies
195
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
177
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 ...
2
votes
2
answers
304
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 ...
-2
votes
2
answers
230
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 ...
1
vote
1
answer
133
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::...
15
votes
1
answer
746
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 ...
5
votes
1
answer
177
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 ...
3
votes
0
answers
96
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 ...
3
votes
3
answers
201
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, ...
1
vote
1
answer
144
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 ...
2
votes
2
answers
140
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) {} //...
0
votes
2
answers
205
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::...
3
votes
2
answers
137
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 ...
2
votes
1
answer
147
views
Casting std::optional inferior type
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::...
1
vote
1
answer
200
views
Undefined class while using __declspec(dllexport) since Visual Studio 17.2
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<...