25 ms·
I prefer C, but I technically write C++ because my “C code” sometimes uses references instead of pointers. (Why did that never become a C feature, anyway? It’s
by derefr 6y ago
I prefer C, but I technically write C++ because my “C code” sometimes uses references instead of pointers. (Why did that never become a C feature, anyway? It’s completely independent from OO/exceptions/templates/etc.) Luckily, in most any environment you can name, installing a C compiler actually implicitly installs a C++ compiler as well; so I can hand my “C code” library to people and tell them it’s C, and the fact that there’re a few .cpp files in there won’t be a problem, any more than having some .asm files in there would be.
I’d like to use std::string as well, but I want my code to externally obey the C FFI, rather than the C++ one (I.e. no need for the linking code to know it needs to initialize the C++ runtime), so I can’t really use any STL. Still wishing C had a standard, portable, found-in-all-compilers prefix-length strings library...
- flohofwoe 6y ago> Why did that never become a C feature, anyway? I would rather ask, why did C++ add another reference type next to pointers ;) PS: the important part is that libraries have a C API, whether the implementation underneath is written in C or C++ isn't all that important. But IMHO once you're restricted to a C API anyway, writing the implementation in C too makes more sense because there's fewer situations where the implementation clashes with the interface.
- pjmlp 6y agoBecause that allows for out parameters and referring to memory addresses without the unsafety of dealing with pointers, something that even ALGOL and PL/I supported.
- 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...
- slavik81 6y ago> I would rather ask, why did C++ add another reference type next to pointers References were added so users could create types that look and feel like the built-in types. You need references to implement operator[] for std::vector or std::deque. Another example would be a vec2 class with a += operator that returns *this, just like the += operator for float.