Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #85903

Re: To C or not to C++

From Manfred <noname@add.invalid>
Newsgroups comp.lang.c++
Subject Re: To C or not to C++
Date 2022-08-13 04:13 +0200
Organization Aioe.org NNTP Server
Message-ID <td71c6$q2e$1@gioia.aioe.org> (permalink)
References (2 earlier) <ygah72lu6nv.fsf@akutech.de> <tcutte$11jl$1@gioia.aioe.org> <tcvpc2$1p184$1@dont-email.me> <td1pb9$68h$1@gioia.aioe.org> <td2qk0$25mc3$1@dont-email.me>

Show all headers | View raw


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:
[...]
>>>
>>>> The fact is that, even if in principle a C code fragment can be 
>>>> compiled as C++ code, which might suggest the two languages are 
>>>> closely related, in truth they are now very different languages, so 
>>>> that they can't be mixed with each other.
>>>
>>> They /are/ very closely related languages - and most well-written C 
>>> code can be turned into working C++ code with only a few changes 
>>> (such as casting the result of calls to malloc - the casts are 
>>> perfectly fine in C, but not idoimatic) and without a change in 
>>> functionality. C is the base language on which C++ builds.
>>
>> I disagree. If you program in C++, i.e. you model a problem domain in 
>> C++ from scratch, then you end up with a /very/ different result than 
>> you would if you used C.
>>
> 
> C++ is multi-paradigm.  There is no single "C++ way" to model a task or 
> program.  It can be done in lots of different ways, depending on the 
> task and the people working on it.  The same applies to a fair extent to 
> C - I have seen plenty of C programs that are object oriented in design, 
> or event based, or have "actors" passing messages between them, or many 
> other ways to organise the C code.  It may be true that much of C 
> programming is "plain imperative" - sets of functions that do things. 
> But it is not restricted to that.
> 
> Now, C++ certainly makes many other types of code structure and task 
> modelling easier - more natural in the language, more efficient to 
> write, more efficient at run-time, and better static checking.  So when 
> deciding how to attack a programming task, you might pick different 
> approaches with C++ than with C, because of different balances in what 
> fits well with the language.  But equally you might not do so - if the 
> problem lends itself to an imperative approach, you can do that in C++ 
> too (and take advantage of better typing and other features that C does 
> not have).
> 

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
- 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.

> 
>>>
>>> And yes they /can/ be mixed, and they /are/ mixed.  C++ is designed 
>>> to be easy to mix with C, and incompatibilities are not made without 
>>> good reason.  Indeed, a good deal of the things people dislike about 
>>> C++, or that are not "good modern language design", trace back to 
>>> compatibility with C.
>>
>> What I mean by "mixed" here is in fact "confused". Nothing to do with 
>> C++ calling C functions, and vice versa (however this might be 
>> accomplished)
>>
> 
> Well, in that sense the languages most certainly /can/ be "mixed" - it 
> is done regularly.  What you mean, I think, is that they should not be 
> mixed - you feel a programmer should either think, model and design in a 
> "C way" and program in C, or they should think, model and design in a 
> "C++ way" and program in C++.
> 
> While I think there is something in that argument, I don't see it as 
> clear-cut or absolute.
> 
>>>
>>> One area of programming where C and C++ are regularly mixed, is 
>>> resource-constrained embedded systems.  You can easily have a project 
>>> with an RTOS in C90, some libraries in C99, application code in C++ 
>>> of some sort, drivers in modern C - a full mix.  Typically the only 
>>> code that is compiled as both C and C++ is the C headers, but you 
>>> sometimes get more complicated headers that provide different 
>>> features depending on the language at the time.
>>
>> Well, I think you agree that a C++ application using a C library is 
>> not really about mixing the two languages, don't you?
>>
> 
> In embedded systems like this, everything is compiled together.  Even if 
> you don't touch the library source code itself, it's common to have 
> hooks, user-defined macros, callbacks, and other connections so that the 
> library code can absolutely be using the programmer's own code.  Plenty 
> of mixing goes on.
> 
>>>
>>>
>>>> If you are writing code that compiles both as C and C++, then you 
>>>> are in fact writing C code, not C++.
>>>
>>> That's just silly.  You are, in fact, writing code that is C code 
>>> /and/ C++ code.  It might not be idiomatic code, but it is perfectly 
>>> valid.
>>
>> Again, I disagree. And it is not silly.
>>
>> Let's take an example that makes some sense: you write some code that 
>> involves a linked list (or any other container, for that matter) - 
>> consider a C implementation and a C++ implementation.
>>
>> Are you seriously arguing that the C implementation is a C++ 
>> implementation just as well?
>> (Please note that I /know/ that the C implementation would be legal 
>> C++ code. I'm just saying that if you write such a C implementation in 
>> a C++ program, then you are Doing It Wrong™)
>>
> 
> 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. I have no doubt that it 
works best in your case, I don't believe that it works best in most cases.
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.

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.
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.

(btw, std::list still exists, it's not banned or anything)

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

To C or not to C++ NOSPAM.Alan.Beck@darkrealms.ca (Alan Beck) - 2022-08-08 09:43 +0000
  Re: To C or not to C++ Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-08 20:02 +0300
  Re: To C or not to C++ Juha Nieminen <nospam@thanks.invalid> - 2022-08-09 05:38 +0000
    Re: To C or not to C++ Ralf Fassel <ralfixx@gmx.de> - 2022-08-09 11:07 +0200
      Re: To C or not to C++ Manfred <noname@add.invalid> - 2022-08-10 02:25 +0200
        Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-09 17:52 -0700
          Re: To C or not to C++ Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-08-09 21:39 -0700
            Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-10 07:45 +0000
          Re: To C or not to C++ scott@slp53.sl.home (Scott Lurndal) - 2022-08-10 19:50 +0000
            Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-10 13:13 -0700
            Re: To C or not to C++ Manfred <noname@add.invalid> - 2022-08-11 04:29 +0200
        Re: To C or not to C++ Juha Nieminen <nospam@thanks.invalid> - 2022-08-10 02:41 +0000
        Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-10 07:39 +0000
          Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-10 10:11 -0700
            Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-12 08:23 +0000
              Re: To C or not to C++ Juha Nieminen <nospam@thanks.invalid> - 2022-08-12 12:12 +0000
                Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-12 14:41 +0000
                Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-12 12:09 -0700
                Re: To C or not to C++ David Brown <david.brown@hesbynett.no> - 2022-08-13 10:28 +0200
                Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-13 09:37 +0000
              Re: To C or not to C++ Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-12 15:55 +0300
                Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-12 14:43 +0000
                Re: To C or not to C++ Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-12 18:07 +0300
                Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-12 15:14 +0000
                Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-12 12:11 -0700
                Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-13 09:38 +0000
                Re: To C or not to C++ David Brown <david.brown@hesbynett.no> - 2022-08-13 17:56 +0200
                Re: To C or not to C++ Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-24 11:32 +0300
                Re: To C or not to C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-24 11:33 +0100
                Re: To C or not to C++ Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-08-24 04:37 -0700
                Re: To C or not to C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-24 13:31 +0100
                Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-24 14:09 +0000
                Re: To C or not to C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-24 15:31 +0100
                Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-24 14:38 +0000
                Re: To C or not to C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-24 15:57 +0100
                Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-24 10:41 -0700
                Re: To C or not to C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-24 21:48 +0100
                Re: To C or not to C++ "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-08-25 07:34 +0200
                Re: To C or not to C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-25 11:29 +0100
                Re: To C or not to C++ Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-08-25 04:14 -0700
                Re: To C or not to C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-25 12:36 +0100
                Re: To C or not to C++ Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-08-25 05:16 -0700
                Re: To C or not to C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-25 17:16 +0100
                Re: To C or not to C++ David Brown <david.brown@hesbynett.no> - 2022-08-25 20:35 +0200
                [OT] Bad CS course, no cookie (Was: To C or not to C++) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-25 21:32 +0100
                Re: [OT] Bad CS course, no cookie (Was: To C or not to C++) David Brown <david.brown@hesbynett.no> - 2022-08-25 23:16 +0200
                Re: [OT] Bad CS course, no cookie Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-26 00:33 +0100
                Re: [OT] Bad CS course, no cookie David Brown <david.brown@hesbynett.no> - 2022-08-26 10:39 +0200
                Re: [OT] Bad CS course, no cookie Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-26 10:57 +0100
                Re: [OT] Bad CS course, no cookie David Brown <david.brown@hesbynett.no> - 2022-08-26 12:49 +0200
                Re: To C or not to C++ Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-08-25 19:56 -0700
                Re: To C or not to C++ David Brown <david.brown@hesbynett.no> - 2022-08-26 10:47 +0200
                Re: To C or not to C++ Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-08-26 02:48 -0700
                Re: To C or not to C++ David Brown <david.brown@hesbynett.no> - 2022-08-26 13:02 +0200
                Re: To C or not to C++ Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-08-26 04:57 -0700
                Re: To C or not to C++ David Brown <david.brown@hesbynett.no> - 2022-08-26 14:55 +0200
                Re: To C or not to C++ Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-08-26 06:20 -0700
                Re: To C or not to C++ Manfred <noname@add.invalid> - 2022-08-27 18:44 +0200
                Re: To C or not to C++ David Brown <david.brown@hesbynett.no> - 2022-08-28 11:30 +0200
                Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-25 10:06 -0700
                Re: To C or not to C++ Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-08-25 10:32 -0700
                Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-25 11:36 -0700
                Re: To C or not to C++ David Brown <david.brown@hesbynett.no> - 2022-08-25 20:47 +0200
                Re: To C or not to C++ Manfred <noname@add.invalid> - 2022-08-26 03:11 +0200
                Re: To C or not to C++ David Brown <david.brown@hesbynett.no> - 2022-08-25 09:21 +0200
                Re: To C or not to C++ scott@slp53.sl.home (Scott Lurndal) - 2022-08-25 12:58 +0000
                Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-25 10:23 -0700
                Re: To C or not to C++ David Brown <david.brown@hesbynett.no> - 2022-08-25 20:51 +0200
                Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-25 12:14 -0700
                Re: To C or not to C++ scott@slp53.sl.home (Scott Lurndal) - 2022-08-24 13:56 +0000
                Re: To C or not to C++ Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-24 17:18 +0300
                Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-24 14:25 +0000
                Re: To C or not to C++ scott@slp53.sl.home (Scott Lurndal) - 2022-08-12 16:46 +0000
                Re: To C or not to C++ Juha Nieminen <nospam@thanks.invalid> - 2022-08-15 06:16 +0000
                Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-15 19:01 +0000
                Re: To C or not to C++ Bo Persson <bo@bo-persson.se> - 2022-08-15 22:22 +0200
                Re: To C or not to C++ David Brown <david.brown@hesbynett.no> - 2022-08-16 08:48 +0200
                Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-16 19:19 +0000
                Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-16 12:56 -0700
                Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-17 18:47 +0000
                Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-17 14:49 -0700
                Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-18 14:49 +0000
                Re: To C or not to C++ Manfred <noname@add.invalid> - 2022-08-19 03:10 +0200
                Re: To C or not to C++ d thiebaud <thiebauddick2@aol.com> - 2022-08-18 22:12 -0400
                Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-18 20:08 -0700
                Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-19 10:39 +0000
                Re: To C or not to C++ Juha Nieminen <nospam@thanks.invalid> - 2022-08-17 06:45 +0000
                Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-17 18:48 +0000
                Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-16 19:16 +0000
              Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-12 12:03 -0700
        Re: To C or not to C++ David Brown <david.brown@hesbynett.no> - 2022-08-10 10:13 +0200
          Re: To C or not to C++ Manfred <noname@add.invalid> - 2022-08-11 04:25 +0200
            Re: To C or not to C++ Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-08-11 01:03 -0700
            Re: To C or not to C++ Juha Nieminen <nospam@thanks.invalid> - 2022-08-11 08:11 +0000
            Re: To C or not to C++ David Brown <david.brown@hesbynett.no> - 2022-08-11 13:53 +0200
              Re: To C or not to C++ Manfred <noname@add.invalid> - 2022-08-13 04:13 +0200
                Re: To C or not to C++ David Brown <david.brown@hesbynett.no> - 2022-08-13 21:13 +0200
                Re: To C or not to C++ Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-08-13 12:29 -0700
                Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-13 13:12 -0700
                Re: To C or not to C++ David Brown <david.brown@hesbynett.no> - 2022-08-14 00:44 +0200
            Re: To C or not to C++ Öö Tiib <ootiib@hot.ee> - 2022-08-12 02:54 -0700
              Re: To C or not to C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-12 12:12 -0700
                Re: To C or not to C++ Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-08-12 16:22 -0700
                Re: To C or not to C++ Öö Tiib <ootiib@hot.ee> - 2022-08-12 17:49 -0700
                Re: To C or not to C++ Manfred <noname@add.invalid> - 2022-08-13 03:16 +0200
                Re: To C or not to C++ Öö Tiib <ootiib@hot.ee> - 2022-08-13 09:08 -0700
                Re: To C or not to C++ Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-08-15 14:44 -0700
        Re: To C or not to C++ Ralf Fassel <ralfixx@gmx.de> - 2022-08-10 12:06 +0200
        Re: To C or not to C++ Juha Nieminen <nospam@thanks.invalid> - 2022-08-11 07:04 +0000
  Re: To C or not to C++ Bonita Montero <Bonita.Montero@gmail.com> - 2022-08-09 12:31 +0200
    Re: To C or not to C++ Muttley@dastardlyhq.com - 2022-08-09 15:59 +0000
    Re: To C or not to C++ Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2022-08-19 04:09 -0700
  Re: To C or not to C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-08-13 12:17 -0700
  Re: To C or not to C++ "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-08-27 23:15 -0700

csiph-web