I am playing with C++23 std::optional
additions, but I can not figure out how to elegantly access the value of object if optional
is active.
I know I can use if
, but that is so C++20.
I really like C++23 API changes, but I can not figure out how to skip the boilerplate of implementing identity. For example:
#include <functional>
#include <iostream>
#include <optional>
void print(std::optional<std::string>& name) {
name.transform([](std::string& x) {
std::cout << x << std::endl;
// I just want to print, without modifying optional, but next line is required
return x;
});
}
int main() {
std::optional<std::string> name{{"Bjarne"}};
print(name);
}
It almost feels like std::optional
is missing invoke
member function.
note: in my example I do not chain anything else after transform, but that is for brevity, I care about optional not being modified.
if
is much easier to read and follow. – Pectintransform
version uses 4 lines of code. Myif
version uses 2. – Pectinif
the compiler doesn’t check that you’re not accidentally using the value in theelse
branch. In other words, it’s utterly pointless. It would be less pointless if the language provided assistance here; e.g. if the compiler complained if we usedx
in theelse
branch in this code:if(auto& x = *opt; opt) … else …;
But unfortunately the language isn’t designed that way. – Materialand_then
member lets you run a function but doesn't modify the optional. If you ignore the return value ofand_then
doesn't it do what you want? en.cppreference.com/w/cpp/utility/optional/and_then – Elegiacand_then
and directly comparing it to an equivalentif
- if you ignore the return code ofstoi
in that example it does what you are after, right? – Elegiacand_then
does for you is to not modify the optional - which wasn't a problem for you because you were returning the same thing you received. You still need to return something. Sorry for not testing my throry before suggesting it. – Elegiacvoid
returningtransform
was considered and rejected – Turinomove
there either. That can just bereturn 0;
– Mender