On 13/08/2022 04:13, Manfred wrote:
> On 8/11/2022 1:53 PM, David Brown wrote:
>> On 11/08/2022 04:25, Manfred wrote:
>>> On 8/10/2022 10:13 AM, David Brown wrote:
>>>> On 10/08/2022 02:25, Manfred wrote:
> [...]
>
> I think the distinction about object-orientation (whose popularity has
> dropped considerably in the last decade or so, btw) or imperative-ness
> of the languages is rather marginal. I agree that both languages can do
> with both just fine.
> But the difference I have i mind goes much farther than that, just
> trivial examples:
> - in C code you use macros everywhere, in C++ you tend to avoid them in
> favor of templates
I don't use macros "everywhere" in C. I know some people do, but it is
certainly not necessary - in fact, my usage of macros in C and C++ is
mostly quite similar.
There is an old-fashioned style of C programming where you use "#define
NO_OF_THINGS 20" and use function-like macros for code size/speed
efficiency. But in modern C you'd use "static const int no_of_things =
20;", or "enum { no_of_things = 20 };" for the purpose, and static
inline functions instead of function-like macros.
Yes, there are things that you can do in C++ with templates that have a
rough equivalent in C that is only possible with macros. (There's also
lots you can do with C++ templates that has no sane equivalent in C.)
But they are not "everywhere". And they are mostly defined once, tucked
away - just like some of the more bizarre C++ definitions. And then
what does it matter? When you use "pow" in your code, it generally
doesn't matter if it is a C++ templated function, a C11 _Generic macro,
or a pre-C11 compiler-specific "magic" macro. (Well, it matters when
you want to make your own quarternion class and provide a power function
for that...)
> - in C you use raw pointers everywhere, in C++ you don't.
> - anywhere templates are the good choice (not because they are fancy or
> because they are C++-ish, but because they behave well for the task at
> hand), you'd use totally different constructs in C.
> - the list obviously can grow ad libitum, I just won't make it long here.
>
C++ has many more features than C. You can do more with the language,
and do things in a way that cannot (sanely) be done in C. I don't think
there are many that would argue differently, and there is little benefit
in listing some of these features.
But if you don't need such features - the code you have can be expressed
simply with functions and simple types - then code written in C++ does
not necessarily look much different from code written in C.
>>
>> I appreciate what you are saying - I just think it is blatantly
>> incorrect to say the code is "C, not C++". What you mean is that it
>> is not /idiomatic/ C++ code. It is not code written in a way that
>> /you/ would write it in C++.
>>
>> C++ is used in a huge range of systems, with a huge range of different
>> requirements. You think that if someone needed a dynamically
>> resizeable list of items, in C++ they'd always reach for a std::vector
>> while in C they'd write it themselves, and therefore the languages are
>> different. When I program in C++, if I needed such a list I'd write it
>> myself, far closer to what you think of as "C style". The standard
>> library std::vector will not do what I need it to do, so I would not
>> use it. But what I write would still be C++.
>
> It's not about idiomatic for the sake of itself, it's about the best
> solution you choose given the problem you have and the tools you have.
>
> If you write a C-style linked list in a C++ program, it's probably due
> to some specifics of the domain you are operating in, rather than a
> solution commonly adopted by C++ programmers.
Yes, I agree. My point is that if I do that, and write a C-style linked
list in C++, then I am writing C++, not C. It might be C++ code that is
structured almost identically to C code - it might even be possible to
compile it as C. But it is still C++.
> I have no doubt that it
> works best in your case, I don't believe that it works best in most cases.
Sure - again, I agree. I am merely objecting to categorical and
artificial divisions, declaring that a piece of code is "not C++" based
on a purely subjective idea of how C++ "should" be written.
> For one, there's a reason raw pointers are seldom used in C++ - not
> because it's the "C++ way", or it's any fancy. It just is because code
> works better this way.
>
I am not convinced on either point. First, I think raw pointers /are/
often used in C++ - much more often, probably, than they should be.
Secondly, I don't think the point is that smart pointers (or containers,
or other ways of managing indirect references) "work better" than raw
pointers. For the most part, they just make it easier to write correct
code, especially in the face of subtle issues such as exception safety.
And they make it harder to write incorrect code, since you can no
longer forget to delete your allocated objects. They don't work faster,
or give smaller code, or compile faster - nor do they let you do
something you could not do before using raw pointers, which are things I
would say would qualify as "work better". Perhaps this is just a
quibble about the wording, but I feel the important point of many C++
features is to improve code quality, safety and correctness above all else.
> As I wrote elsethread, I am not saying that C++ is better than C in an
> absolute sense. I am convinced that the choice between the two languages
> depends entirely on the problem domain: in some cases C is better, in
> other it's the other way around.
Agreed.
> However, if C++ is chosen because it is a better solution for the
> problem at hand, then I see that, unsurprisingly, you end up writing
> code that is quite different from C.
>
The choice of language is often not made on purely technical "it's a
better solution" grounds. But while I think that your argument is valid
for a lot of code, I don't think it /always/ applies - and I don't think
it is appropriate to categorise all C++ code that is structured like a C
solution as somehow not "real" C++.