Is C++ passing by value by default considered a design flaw?
Being new to the language and reading lots of books (like the Effective series) I feel like I am spending half the time learning about ways to pass by reference, ways to avoid the pitfalls when passing by reference (dangling pointers and so on), deleting copy constructors, and all the subtly devious ways in which something might actually be copied under the hood even when you didn't intend for it to happen.
So my question is is there a reason it was designed to work this way when most of the time you intend to pass by reference anyway? It seems that languages like C# indeed work the other way around, so I am wondering whether this is considered a design flaw in the programming community or if there's actually a good reason for it?
Posted Sep 7, 2023, 12:37 AM UTC
C++ has plenty of flaws, but passing by value isn't one of them.
You typically know when you're juggling things you maybe shouldn't blindly copy, and proceed accordingly.
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-conventional