Questions tagged [std-expected]

The C++ class std::expected provides a way to store either either an expected value, or an unexpected value, which is often an error code or error message. Functions that can fail often return std::expected to either return a result, or error information.

10 questions
28
votes
1 answer

What is std::expected in C++?

In one of the most respected stackoverflow answer I found an example of std::expected template class usages: What are coroutines in C++20? At the same time I cannot find any mentioning of this class on cppreference.com. Could you please explain what…
Fedor
  • 17,146
  • 13
  • 40
  • 131
5
votes
0 answers

When to use std::expected instead of exceptions

When should I use std::expected and when should I use exceptions? Take this function for example: int parse_int(std::string_view str) { if (str.empty()) { throw std::invalid_argument("string must not be empty"); } /* ... */ …
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
5
votes
1 answer

Why are there no monadic operations in std::expected?

In C++23, monadic operations in std::optional was adopted, and later on std::expected. Why were monadic operations like and_then, transform, and or_else not included in the main paper?
Desmond Gold
  • 1,517
  • 1
  • 7
  • 19
3
votes
1 answer

std::unexpected constructor constraint

I am reading the cpp 23 standard and I stumbled upon std::unexpected. Section expected.un.cons defines template constexpr explicit unexpected(Err&& e); with the following costraint (among others) is_same_v,…
NotMe
  • 65
  • 5
2
votes
1 answer

How to avoid calling the destructor twice when using a factory function with a std::expected return without runtime costs

It's about embedded C++. Let's assume I have a struct Timer_t. To create a new object the constructor is private we have a factory-function as a public member makeTimer() We can't use exceptions on the device and to intiailize the Timer we can get…
Sickeroni
  • 66
  • 6
2
votes
2 answers

C++ is there an implementation of std::expected that doesn't consider a default constructed `expected` to have a value?

I've been looking for a good way to improve error handling in a C++ library, with the goal of reducing the risk of bug-prone code while maintaining efficiency. I stumbled across Andrei Alexandrescu's Expect the Expected talk on YouTube and am very…
Tom
  • 18,685
  • 15
  • 71
  • 81
1
vote
1 answer

Is that possible to upcast erased type without RTTI?

I have a custom implementation of std::expected, but with a type-erased error. It makes the expected type looks similar to exceptions. We just can use the next code: Expected V = 123; V.SetError(std::string("Error occurred")); And when we try…
Artem Selivanov
  • 1,867
  • 1
  • 27
  • 45
1
vote
1 answer

How to use std::expected in conjunction with std::transform?

I have a flatbuffer. This flatbuffer has a vector that I want to transform using std::transform into a std::vector. It is possible that T can contain values that are not valid - because they are a plain integer converted to an enum. In this…
Raildex
  • 3,406
  • 1
  • 18
  • 42
1
vote
1 answer

std::expected, references, and std::reference_wrapper

This question is related to std::expected and reference return type I try to get my head around std::expected (respectively https://github.com/TartanLlama/expected) as an alternative error handling methodology, and I like the concept. However,…
NeitherNor
  • 254
  • 2
  • 7
0
votes
0 answers

error: expected initializer before 'std' and other errors after adding one line to working code

The following program worked already before I added line 12, the line reading: " std::cout<<" Natürlicher Logarithmus von ";std::cin>>X; ". Now I get a lot of errors (see below)! Please Help // Berechnung des natürlichen Logarithmus (Grenzwert…
fribir
  • 1