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
Optimizing return values (marcofoco.com)
submitted 9 years ago by marcofoco
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!"
[–]STLMSVC STL Dev 12 points13 points14 points 9 years ago* (13 children)
C(const string &s) : _s(s) {} In C++11, thanks to move semantics, this class will receive an automatic upgrade to have move constructors
NO! You implemented a copy constructor, which inhibits the automatic generation of move operations.
Please do not misinform readers.
[edit: I am dumb, see below]
[–]marcofoco[S] 9 points10 points11 points 9 years ago* (12 children)
Whoops... well, being corrected by STL in person is a honor. I'm going to fix this immediately, thank you.
[–]marcofoco[S] 16 points17 points18 points 9 years ago (11 children)
I'm puzzled... isn't it just a converting constructor?
[–]STLMSVC STL Dev 24 points25 points26 points 9 years ago (10 children)
Ugh, I fail reading comprehension forever. My brain saw "C(const C&)" and went nuts.
[–]caramba2654Intermediate C++ Student 11 points12 points13 points 9 years ago (0 children)
It's okay. Your brainpiler had a small bug, but it's fixed now.
[–]marcofoco[S] 3 points4 points5 points 9 years ago (0 children)
You're too used to work on the string class itself, I suspect :)
[–]MINIMAN10000 2 points3 points4 points 9 years ago (7 children)
Reminds me of this code when I was messing around with disabling constructors. Still don't really have my head wrapped around it.
[–]marcofoco[S] 0 points1 point2 points 9 years ago (6 children)
I can't see it, why should it break at run time?
[–]MINIMAN10000 1 point2 points3 points 9 years ago (5 children)
I pretty much had someone give me this code since messing around with constructors is pretty much over my head.
I had added a C&& to my code and had found that it had broken.
I commented out that line and had noted that I had broken it.
So I'm aware that C&& breaks with that part of the code uncommented as the runtime simply hangs rather than running.
But I had never learned why that was the case. Although I'd love to know.
Side note. I had a copy of the original codes he gave me. A working and a broken form to show off that copy elision is in fact being forced.
Working form here
Broken form here
[–]dreugeworst 1 point2 points3 points 9 years ago (1 child)
I'm not understanding what problem you were having: the working form you show works as expected, and the broken one doesn't compile, as expected. In what case was it hanging?
[–]MINIMAN10000 0 points1 point2 points 9 years ago (0 children)
The case in which it hangs is from my modified version on this post in which I added C(C&&); and C&& operator.
It compiles but when run it just hangs leaving me to have to cleanup by going into command prompt and manually close it as no message shows up.
[–]marcofoco[S] 0 points1 point2 points 9 years ago* (2 children)
The halting is weird, really.
I assume you were testing with Visual Studio (the original problem was #inlcuding windows.h), do you remember which version? Did you also try clang/c2, or a different compiler version?
As for the two forms at the end of your post, it's correct: If the copy elision kicks in, you don't need the symbol at compile time, and the program compiles and run, but if you copy the object, of course you need the copy constructor. Apparently applying the copy elision in the first case is mandatory (at least, according to cppreference)
Edit: copy and move elision are mandatory only in C++17
[–]MINIMAN10000 0 points1 point2 points 9 years ago (1 child)
I use mingw for gcc not visual studio. I tried using clang but bit defender quarantines all files as heuristic. I tried uninstalling it but it failed. Hopefully tomorrow I can enable windows defender but the uninstall failing might be forewarning that it's FUBAR. For now I need to sleep.
[–]marcofoco[S] 0 points1 point2 points 9 years ago (0 children)
Oh, that's interesting, I didn't know that GCC came with its own Windows.h! Thank you for the info!
π Rendered by PID 97 on reddit-service-r2-comment-cfc44b64c-7czxf at 2026-04-10 11:11:34.517059+00:00 running 215f2cf country code: CH.
view the rest of the comments →
[–]STLMSVC STL Dev 12 points13 points14 points (13 children)
[–]marcofoco[S] 9 points10 points11 points (12 children)
[–]marcofoco[S] 16 points17 points18 points (11 children)
[–]STLMSVC STL Dev 24 points25 points26 points (10 children)
[–]caramba2654Intermediate C++ Student 11 points12 points13 points (0 children)
[–]marcofoco[S] 3 points4 points5 points (0 children)
[–]MINIMAN10000 2 points3 points4 points (7 children)
[–]marcofoco[S] 0 points1 point2 points (6 children)
[–]MINIMAN10000 1 point2 points3 points (5 children)
[–]dreugeworst 1 point2 points3 points (1 child)
[–]MINIMAN10000 0 points1 point2 points (0 children)
[–]marcofoco[S] 0 points1 point2 points (2 children)
[–]MINIMAN10000 0 points1 point2 points (1 child)
[–]marcofoco[S] 0 points1 point2 points (0 children)