Questions tagged [stdoptional]

In C++ the std::optional class template represents an optional value, i.e. one that may or may not be present.

The C++ class template std::optional<T> represents an optional value of type T, so that either it contains a valid value of type T or it contains nothing.

Related tags

173 questions
5
votes
1 answer

Why does std::optional operator* not have debug mode assertion for has_value()?

I perfectly understand that because of performance reasons the operator* in std::optional does not make any checks for the actual existence of a contained value. However, in debug mode performance considerations should not matter and it would make a…
5
votes
3 answers

Use of std::optional to pass a std::vector to a functional by reference

I am unclear if the correct code to pass an optional vector of ints to a function by reference is : void test_func(std::optional&> vec) or void test_func(std::optional>& vec) Any help much appreciated.
oracle3001
  • 1,090
  • 19
  • 31
5
votes
1 answer

Does C++ standard allow std::optional to be implemented without overhead

I just watched cppcon talk about Bloomberg datum, variant type that uses redundancy in IEEE754 format to encode what type is stored in datum. So I was wondering does C++ standard allow for implementations to implement std::optional more efficiently…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
5
votes
2 answers

Why std::optional::value() &&; return &&?

I have had runtime error, when replaced some code by using std::optional: Old code: T getValue(); ... const auto& value = getValue(); value.get(); New code: std::optional getValue(); ... const auto& value = getValue().value(); value.get(); //…
Draks
  • 323
  • 2
  • 9
4
votes
1 answer

std::optional::transform with std::addressof

Consider the following code: #include #include template constexpr T * arg_to_pointer(T & arg) noexcept { return std::addressof(arg); } int main() { std::optional foo; auto * test =…
R K
  • 89
  • 5
4
votes
1 answer

How can I get clang-tidy to not complain about passing kind-of-lightweight types by value?

CLion (2023.1.2) is running clang-tidy v17 on a project of mine. It's complaining about me passing an std::experimental::optional (from C++14) by value instead of by reference. That seems egregious to me... how can I increase the "object weight…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
4
votes
2 answers

prevent initializing std::optional> with rvalue std::optional

std::reference_wrapper cannot be bound to rvalue reference to prevent dangling pointer. However, with combination of std::optional, it seems that rvalue could be bound. That is, std::is_constructible_v, int&&>) is…
slyx
  • 2,063
  • 1
  • 19
  • 28
4
votes
1 answer

Does std::optional<>::emplace() invalidate references to the inner value?

Consider the following fragment (assume that T is trivially constructible and trivially destructible): std::optional opt; opt.emplace(); T& ref = opt.value(); opt.emplace(); // is ref guaranteed to be valid here? From the definition of…
Pavel Kirienko
  • 1,162
  • 1
  • 15
  • 31
4
votes
3 answers

What are the advantages/disadvantages of std::optional over nullptr?

I looked several online std::optional documentary over the internet. However I could not be able to find any direct comparison between two cases below: case 1: SomePointer* foo::get_some_pointer(cont int value) { auto result =…
Meric Ozcan
  • 678
  • 5
  • 25
4
votes
2 answers

Are std::optional members stored contiguously?

I suppose I'm a bit confused as to how exactly optional values are stored. When constructing a class or struct that contains std::optional members, will these members be stored contiguously in memory or does optional allocate dynamically? For…
whitwhoa
  • 2,389
  • 4
  • 30
  • 61
4
votes
1 answer

How to use second overload of std::optional::emplace

In the std::optional::emplace docs there is an overload that accepts std::initializer_list: template< class U, class... Args > T& emplace( std::initializer_list ilist, Args&&... args ); provided that std::is_constructible
pptaszni
  • 5,591
  • 5
  • 27
  • 43
4
votes
2 answers

Why is `std::optional::operator=` deleted when T contains a `const` data member?

The following code would lead to compiler errors: #include class A { }; class B { private: const A a; }; int main() { B b; std::optional bo1; bo1 = b; } On gcc, for example, the error reads: main.cpp: In function…
DXZ
  • 451
  • 4
  • 14
4
votes
2 answers

How to get std::optional support in Xcode?

I'm trying to use std::optional in an Xcode 12.0 Mac OS project. I'm getting the error: No template named 'optional' in namespace 'std' #include std::optional o; My settings are (I need libc++ for project):
bhartsb
  • 1,316
  • 14
  • 39
4
votes
1 answer

Can I somehow return std::optional with const-reference to my data to avoid copy of it?

Suppose I have some base class which can optionally return me some specific data. It also provides me 'hasData' function to check if such specific data is available for usage class MyClassBase { public: virtual bool hasData() const { return…
Michael Nosov
  • 61
  • 1
  • 4
4
votes
2 answers

Why std::string append is not overloaded on rval ref?

I'm familiar, that the append in std::string returns std::string& and therefore it do not qualify to be moved from, so the result will not be moved. #include int main() { std::string s = std::string("A").append("B"); return…
koscelansky
  • 153
  • 8