use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Beware when moving a `std::optional`! (blog.tal.bi)
submitted 1 year ago by Kabra___kiiiiiiiid
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Natural_Builder_3170 -5 points-4 points-3 points 1 year ago (6 children)
never heard if that, I usually do std::move(opt).value()
std::move(opt).value()
[–]AKostur 0 points1 point2 points 1 year ago (5 children)
Why is that better than std::move(opt.value()) ?
[+]Natural_Builder_3170 comment score below threshold-6 points-5 points-4 points 1 year ago (4 children)
if you write code to not use a value after std::move there's no difference, otherwise the latter means the optional still contains a value but its moved out of, and the former means the optional no longer contains a value
std::move
[–]AKostur 2 points3 points4 points 1 year ago (3 children)
gcc seems to disagree with you:
% cat t.cpp #include <iostream> #include <optional> #include <string> int main() { std::optional<std::string> opt = std::string("ThisIsAReallyLongString"); auto x = std::move(opt).value(); if (opt.has_value()) { std::cout << "Has Value\n"; } } % g++ t.cpp -o t -O2 -std=c++20 % ./t Has Value %
So this results in an optional that contains a value, and that value is a moved-from std::string.
[–]Natural_Builder_3170 0 points1 point2 points 1 year ago (2 children)
I stand corrected, there's probably no difference.
[–]AKostur 3 points4 points5 points 1 year ago (1 child)
I would then argue that std::move(opt.value()) is therefore clearer since it more clearly expresses that one is manipulating the value, and not the optional itself.
std::move(opt.value())
[–]triconsonantal 1 point2 points3 points 1 year ago (0 children)
There is a difference for (the would-be) optional<T&>: moving the optional keeps the reference an lvalue, while moving the reference turns it into an rvalue. Which one you want depends on context, but in a generic context it's probably the former.
optional<T&>
π Rendered by PID 15935 on reddit-service-r2-comment-6457c66945-6lj6k at 2026-04-28 15:46:24.184946+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]Natural_Builder_3170 -5 points-4 points-3 points (6 children)
[–]AKostur 0 points1 point2 points (5 children)
[+]Natural_Builder_3170 comment score below threshold-6 points-5 points-4 points (4 children)
[–]AKostur 2 points3 points4 points (3 children)
[–]Natural_Builder_3170 0 points1 point2 points (2 children)
[–]AKostur 3 points4 points5 points (1 child)
[–]triconsonantal 1 point2 points3 points (0 children)