216 questions
Best practices
0
votes
7
replies
154
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 ...
2
votes
2
answers
286
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 ...
1
vote
1
answer
127
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::...
5
votes
1
answer
175
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
3
answers
198
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, ...
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) {} //...
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 ...
1
vote
1
answer
198
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<...
15
votes
2
answers
1k
views
Can I have a std::optional<T> if T is neither constructible nor copyable nor movable?
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 ...
0
votes
0
answers
43
views
OpenCV C++ Temporary RValue Class Reference Invalidated by std::optional member?
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). ...
3
votes
1
answer
102
views
Is it ok to double-dereference ("**itOpt") an optional iterator?
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::...
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 right away?
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<...
0
votes
0
answers
83
views
Does optional<reference_wrapper> not return a reference?
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 ...
0
votes
1
answer
192
views
Is it useful to construct an std::optional using std::in_place when returning from a function?
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 ...
-4
votes
1
answer
700
views
error: cannot convert 'std::optional<int>' to 'const int' in initialization
#include <vector>
#include <ranges>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
const vector<int> v = {1, 2, 3};
const int n = ...
0
votes
2
answers
84
views
Writing functions which handle invalid inputs in C++
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-...
2
votes
1
answer
187
views
Pass-through constructor for std::optional argument
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 ...
1
vote
2
answers
103
views
Is there another way to achieve the functionality of std::optional without another object being created?
class ScopedClass {
public:
ScopedClass() {
cout << "constructor called\n";
}
~ScopedClass() {
cout << "destructor called\n";
}
ScopedClass(const ...
0
votes
2
answers
273
views
Getting an optional of a class derived from abstract class, from a class derived from abstract class
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 ...
1
vote
1
answer
58
views
Value corrruption with a view pointing to a value within an std::optional
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 ...
0
votes
0
answers
91
views
std::optional<T> assignment operators
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 ...
1
vote
1
answer
459
views
Pass `std::optional::value` to `std::views::transform`
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 <...
2
votes
1
answer
103
views
What's the point of ref-qualified member functions in `std::optional::value`
These are the signatures, according to Cppreference:
constexpr T& value() &;
constexpr const T& value() const &;
constexpr T&& value() &&;
constexpr const T&& ...
1
vote
1
answer
1k
views
Forward declaration of class inside unique_ptr inside optional with default argument fails
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 ...
0
votes
2
answers
3k
views
optional refence to an object: best way?
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 ...