Return value optimization is absolutely allowed in C, and ~all compilers do it. It's not commonly talked about because in C, there can be no side-effects when constructing or copying a struct, so there's nothing to discuss. It just happens automatically and there's no observable change in program behavior.
C compilers can do copy ellision when inlining code as long as the result is identical. That's not quite the same thing as RVO, which deliberately relaxes the notion of "identical result" such that it's possible to write C++ code to detect whether or not RVO was actually performed. That's not possible in C (or shouldn't be).
Modern C++ for C Programmers: part 5
21–30 of 82 posts
Re: Modern C++ for C Programmers: part 5
#22Please please please can we get rid of headers and have modules? I find it super annoying having to specify things half in one place and half in another. Unfortunately, there still seems to be a bit of disagreement on the implementation among the standards committee.
Re: Modern C++ for C Programmers: part 5
#23> The move constructor is the important bit. Its presence tells C++ that this class can not be copied, only moved Is that true? I thought move ctor enabled move semantics, but if you pass by value the copy ctor was still called (except in places where RVO makes sense). And I thought to disable copying you made the copy ctor = delete.
The implicitly-declared copy constructor is deleted if there is a user-defined move constructor. See: https://en.cppreference.com/w/cpp/language/copy_constructor#... So if you define a move constructor and still want your class to have a copy constructor, you'll have to explicitly define the copy constructor, too.
A::A(const A &other) = default;Re: Modern C++ for C Programmers: part 5
#24is there a modern C++ for python programmers? i have ~5 years programming in dynamic/interpreted (python/js) and compiled/gc'd(java/go) languages and i'd like to learn a systems language. i'm reading the rust programming book (and it's really good/easy to grok) but i'd also like to learn C++. the problem is that most books are either too easy (C++ as your first language) or too hard (straight into RAII and templates)…
I figure you didn't mean to call it out specifically and more meant general smart pointers, but just felt I'd let you know that searching for that terminology may lead you astray.
Re: Modern C++ for C Programmers: part 5
#25> The move constructor is the important bit. Its presence tells C++ that this class can not be copied, only moved Is that true? I thought move ctor enabled move semantics, but if you pass by value the copy ctor was still called (except in places where RVO makes sense). And I thought to disable copying you made the copy ctor = delete.
"The implicitly-declared or defaulted copy constructor for class T is defined as deleted if any of the following conditions are true ... T has a user-defined move constructor or move assignment operator" (since C++11) https://en.cppreference.com/w/cpp/language/copy_constructor But there's no harm in explicitly deleting it, and it helps document the intent.
Re: Modern C++ for C Programmers: part 5
#26Earlier quoted context omitted.
C compilers can do copy ellision when inlining code as long as the result is identical. That's not quite the same thing as RVO, which deliberately relaxes the notion of "identical result" such that it's possible to write C++ code to detect whether or not RVO was actually performed. That's not possible in C (or shouldn't be).
It's possible that I'm missing something, but I believe that the result is necessarily identical in C, because there can be no observable side-effect of a struct copy in the C abstract machine.
Re: Modern C++ for C Programmers: part 5
#27Earlier quoted context omitted.
"The implicitly-declared or defaulted copy constructor for class T is defined as deleted if any of the following conditions are true ... T has a user-defined move constructor or move assignment operator" (since C++11) https://en.cppreference.com/w/cpp/language/copy_constructor But there's no harm in explicitly deleting it, and it helps document the intent.
What's the motivation behind this?
Perhaps if you were designing C++ from scratch without legacy and had a more Python-like mindset, you'd require an explicit "= default" rather than complicated rules about whether a default is provided for you or not.
Re: Modern C++ for C Programmers: part 5
#28I wish I had a project that warranted C++ right now. It’s a magnificent language.
Re: Modern C++ for C Programmers: part 5
#29Please please please can we get rid of headers and have modules? I find it super annoying having to specify things half in one place and half in another. Unfortunately, there still seems to be a bit of disagreement on the implementation among the standards committee.
Having a reasonable module system, and hell even some lovely tools for migrating sane projects over to it, would make me much happier with the language.
For what it's worth, Clang has experimental support for C++ modules.