Skip to main content
Filter by
Sorted by
Tagged with
Best practices
0 votes
7 replies
154 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
2 votes
2 answers
286 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
1 vote
1 answer
127 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
5 votes
1 answer
175 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
3 answers
198 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
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
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
1 vote
1 answer
198 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 votes
2 answers
1k views

The only requirement to type parameter of std::optional<T> class template mentioned in [optional.optional.general] p3 is that type T is Destructible. Suppose I have a very restrictive class ...
Igor G's user avatar
  • 2,668
0 votes
0 answers
43 views

I believe the source of this is actually some C++ craziness, specifically with std::optional, but the context came from OpenCV. OpenCV has a datastructure, cv::Mat, and some variants (for gpu, etc). ...
JWCS's user avatar
  • 1,223
3 votes
1 answer
102 views

If I have a std::optional wrapped around (say) an std::vector::const_iterator, is it safe to access the referenced element with two consecutive dereference (*) operators? For example: typedef std::...
Scott McPeak's user avatar
  • 13.8k
4 votes
3 answers
223 views

Why is it undefined behavior to call operator* on an empty std::optional if I don't access the returned value before emplacing an object? For example, why is the following code UB? std::optional<...
Adel M.'s user avatar
  • 500
0 votes
0 answers
83 views

I wanted to make a Table class that is supposed to work like a table with columns and rows, and values in the cells. Obviously I need a function that would allow to get the value from a particular ...
Marcin Pagórek's user avatar
0 votes
1 answer
192 views

I was just wondering, is it useful to return an std::optional from a function constructed using std::in_place? Should I be doing it blindly or does copy elision/changing C++ versions/the data ...
Alexandre Deus's user avatar
-4 votes
1 answer
700 views

#include <vector> #include <ranges> #include <algorithm> #include <functional> using namespace std; int main() { const vector<int> v = {1, 2, 3}; const int n = ...
Ludovic Aubert's user avatar
0 votes
2 answers
84 views

In C++, how to handle function outputs , if the inputs are invalid. Say, I am writing a function to multiply two matrices. How to deal with it if the two matrices cannot be multiplied (due to in-...
Vedant Yadav's user avatar
2 votes
1 answer
187 views

Is there a way, idiomatically, to provide a constructor/conversion which takes a std::optional<T> and returns a std::optional<U>? For instance, ideally I would love some kind of syntax ...
kc9jud's user avatar
  • 402
1 vote
2 answers
103 views

class ScopedClass { public: ScopedClass() { cout << "constructor called\n"; } ~ScopedClass() { cout << "destructor called\n"; } ScopedClass(const ...
Jo3kerR's user avatar
  • 43
0 votes
2 answers
273 views

I have something similar to the code below, where a group of classes with similar shared behaviours (Tool1, Tool2), all inherit from an abstract class (ITool). All these classes own their own optional ...
bmetz's user avatar
  • 15
1 vote
1 answer
58 views

I am trying to create a setting where an instance of a C++ class optionally owns the data. For this I'm creating an std::optional<Type>, and a view to this optional (or something supplied from ...
jerin's user avatar
  • 653
0 votes
0 answers
91 views

My reference is to options (4), (5) and (6) of std::optional::operator= Given the premise that The class template std::optional manages an optional contained value, i.e. a value that may or may not be ...
Vinod's user avatar
  • 1,215
1 vote
1 answer
459 views

I cannot seem to pass std::optional::value to std::views::transform. However, I can pass std::optional::has_value without any issues: #include <optional> #include <ranges> #include <...
antonio's user avatar
  • 483
2 votes
1 answer
103 views

These are the signatures, according to Cppreference: constexpr T& value() &; constexpr const T& value() const &; constexpr T&& value() &&; constexpr const T&& ...
BIuesky's user avatar
  • 107
1 vote
1 answer
1k views

I have the following piece of code // DoSomething.h class Foo; void doSomething(std::optional<std::unique_ptr<const Foo>> f = std::nullopt); If I include DoSomething.h anywhere without ...
Michael's user avatar
  • 282
0 votes
2 answers
3k views

I want a function to return an optional reference to an object. Idea is to avoid copy, but seems like i should not be using according to this discussion: std::optional specialization for reference ...
mu5e's user avatar
  • 135

1
2 3 4 5