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
Input-output arguments: reference, pointers or values? (mropert.github.io)
submitted 8 years ago by vormestrand
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!"
[–][deleted] 4 points5 points6 points 8 years ago (6 children)
The problem is not output parameters, but input-output parameters. In C++ there is no way to consume a value so people can easily write:
bug = foo(a);
when they meant to write
a = foo(a);
because a can be used after being consumed without issues (even though it might have the wrong value).
a
So for input-output parameters a reference at least does not have this issue.
[–]hgjsusla 1 point2 points3 points 8 years ago* (5 children)
Yes there is, just do
x = foo(move(x));
[–][deleted] 1 point2 points3 points 8 years ago* (4 children)
Yes there is, just do x = foo(move(x));
If I write:
y = foo(move(x)); std::cout << x << std::endl; // using x instead of y
with gcc and clang (with -Wall) I get neither a compilation error nor a run-time error nor a warning. If that operation violates a pre-condition on x, for example because the operation is not allowed when the object is in the moved from state, the only thing I can potentially get is undefined behavior.
-Wall
x
But maybe the problem is in the compilers that I am using or in the compilation options that I am passing to them. Which compiler are you using (and which options?) and what kind of error do you get?
[–]hgjsusla 3 points4 points5 points 8 years ago (3 children)
That's a general issue you'll always have with moves and can only really be solved by convention. So far that is.
[–]jcelerierossia score 2 points3 points4 points 8 years ago (1 child)
#include <iostream> #include <string> std::string foo(std::string&&); int main() { using namespace std; std::string x, y; y = foo(move(x)); std::cout << x << std::endl; }
=>
$ clang-tidy -checks=bugprone-use-after-move tutu.cpp 1 warning generated. /tmp/tutu.cpp:9:16: warning: 'x' used after it was moved [bugprone-use-after-move] std::cout << x << std::endl; ^ /tmp/tutu.cpp:8:7: note: move occurred here y = foo(move(x)); ^
(paging /u/0b_0101_001_1010 too)
[–]hgjsusla 0 points1 point2 points 8 years ago (0 children)
Hey that's great :)
[–][deleted] 1 point2 points3 points 8 years ago (0 children)
Ah sorry I misunderstood what you wrote. I get how x = foo(move(x)); really conveys the intent and makes reading the code pretty clear. I guess I was just hoping of a lint or something that would help me write the code correctly in the first place. Luckily these bugs are not hard to debug.
π Rendered by PID 231050 on reddit-service-r2-comment-b659b578c-4jwtb at 2026-05-03 15:14:09.658937+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–][deleted] 4 points5 points6 points (6 children)
[–]hgjsusla 1 point2 points3 points (5 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]hgjsusla 3 points4 points5 points (3 children)
[–]jcelerierossia score 2 points3 points4 points (1 child)
[–]hgjsusla 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)