2 ms·
> allows for out parameters Out parameters are mostly a legacy feature from the time when C compilers didn't allow struct return values though, or implemented
by flohofwoe 6y ago
> allows for out parameters
Out parameters are mostly a legacy feature from the time when C compilers didn't allow struct return values though, or implemented this inefficiently (besides, out parameters via pointers works too).
> memory addresses without the unsafety of dealing with pointers
I never understood the "increased safety" argument. C++ references can become dangling just as easily as pointers, and arguable they're even worse, because most of the time they look like regular value types.
- pjmlp 6y agoOut parameters are a welcomed feature of all memory safe systems programming languages, only missed from C and BCPL. C++ references surely are safer than pointer that can point to whatever they feel like and aren't initialized to any safe value. There is a difference between being 100% fully safe, which they aren't, and being safer than plain unsafe pointers Assembly style.
- jstimpfle 6y agoAlthough I've never found it useful, I believe if you do TYPE *const ptr = foo(); to initialize a C pointer where you can't change what thing it points to (as opposed to the thing itself), then you get 100% of the alleged safety benefits that you get from C++ references. Except, you're not adding another type category in the type system, with all the complexities (non-orthogonality of features) that come with that. Granted, compared to C++ reference declarations like TYPE& ptr = foo(); you'll have to type a few characters more, but then again you won't have to repeat your method signatures in 18 different flavours.
- pjmlp 6y agoOn those cases not, but certainly as function parameters. or class members that are initialized on the constructor and never change during the class lifetime, if they are pointers instead it may raise lifetime questions. Althought probably the correct way would be to make use of std::observer_ptr<>(), but it still isn't in the standard.
- cygx 6y ago> C++ references surely are safer than pointer that can point to whatever they feel like and aren't initialized to any safe value. From the C programmer's perspective, a reference is a constant pointer with automatic indirection. As far as 'safety' is concerned, there's not much difference between int& ref = ...; and int *const ptr = ...; except in the most trivial of cases. edit: However, this of course does not mean that references are useless within the C++ context - they do have an effect on how you write your code...
- benibela 6y agoEasier to write . than ->
- jstimpfle 6y agobut less clear. Some of the newer languages do aways with -> completely but I think it just obscures the data structures. The one frequent annoyance that I had with . vs -> is that for access local variables I need to use . and if I make that code into a function that takes the local variables as a pointer, I need to change the code into ->. In other words, I can't easily move the code through stages of solidification using simple copy & paste. That's why I tend to declare my local variables using pointer literal syntax, e.g. Foo *local_thing = &(Foo) { ... }; That way it gets easier to move the code out.
- jcelerier 6y ago> Out parameters are mostly a legacy feature from the time when C compilers didn't allow struct return values though, or implemented this inefficiently (besides, out parameters via pointers works too). ah yes, that legacy, age-old GCC 11 compiler https://gcc.godbolt.org/z/rYsxxTxqd https://gcc.godbolt.org/z/rYsxxTxqd
- syockit 6y agoWow. The function produces identical code when inlined, so why does the C version require that memcpy when it's not inlined?
- jcelerier 6y agobecause C++ allows RVO and not C (as this is technically a change in behaviour as one may instrument memcpy - there are also a few cases where the copying of the return value of a function is observable so this does not fall under the default "as-if" rule of optimization)
- flohofwoe 6y agoClang produces identical code between C++ and C: https://gcc.godbolt.org/z/vWqn4s5ev https://gcc.godbolt.org/z/vWqn4s5ev I don't think that C disallows RVO, gcc just might be more conservative there (maybe because of special Linux requirements)?
- jcelerier 6y ago> I don't think that C disallows RVO, optimizations should never change the output of a program unless explicitely allowed (the as-if rule). In c++ since RVO is allowed, it is ok for intptr_t value = 0; struct foo f() { struct foo the_foo; value = (intptr_t)&the_foo; return the_foo; } int main() { struct foo the_foo = f(); assert(value == (intptr_t)&the_foo); } to assert or not depending on the -O level. But I'd wager that it is not the case in C.