Re: The C versus C++ debate
Posted: Wed Aug 18, 2010 7:01 am
C++ has ground-up designed constexpr's to handle those with just a bit more elegance.
The Place to Start for Operating System Developers
https://f.osdev.org/
I'm not sure about the rules for recursion with constexprs, though IIRC there was a defect report about it.Candy wrote:C++ has ground-up designed constexpr's to handle those with just a bit more elegance.
It's amazing how people get the post wrong. The suggestion was that one would be safe if you used C++ as "C with classes", and not that C++ in fact was that, and it's pretty much true. The reasons you would end up with larger or slower executable with C++ than C is that you're using features of C++ that is not in C - otherwise you should end up with the same result (at least if you turn of exception handling and RTTI in the compiler). Templates for example can easily lead to code bloat if you don't take care to avoid code duplication (which can mean that you're using memory less effectively)...pcmattman wrote: Well, unless you just stepped out a Delorean, C++ is definitely not C with classes. Who really cares what it was when it's definitely not that right here in 2010?
Calling C++ "C with classes" is a really easy way to get out of writing decent C++, or even to fight against the language. You can tear the language to pieces if all it is to you is "C with classes". When you consider the other features (in my opinion the main feature of interest is templates) you start entering territory you can't come close to handling in C. Sure, you can make a "generic" linked list implementation in C that works off void pointers... but in C++ that can be fully type-checked at compile time, and use memory more effectively, with a template.
If you just use C++ as C with classes, and throw out templates and exceptions, then you throw out much of what makes C++ nice to work with.skyking wrote:It's amazing how people get the post wrong. The suggestion was that one would be safe if you used C++ as "C with classes", and not that C++ in fact was that, and it's pretty much true. The reasons you would end up with larger or slower executable with C++ than C is that you're using features of C++ that is not in C - otherwise you should end up with the same result (at least if you turn of exception handling and RTTI in the compiler). Templates for example can easily lead to code bloat if you don't take care to avoid code duplication (which can mean that you're using memory less effectively)...pcmattman wrote: Well, unless you just stepped out a Delorean, C++ is definitely not C with classes. Who really cares what it was when it's definitely not that right here in 2010?
Calling C++ "C with classes" is a really easy way to get out of writing decent C++, or even to fight against the language. You can tear the language to pieces if all it is to you is "C with classes". When you consider the other features (in my opinion the main feature of interest is templates) you start entering territory you can't come close to handling in C. Sure, you can make a "generic" linked list implementation in C that works off void pointers... but in C++ that can be fully type-checked at compile time, and use memory more effectively, with a template.
IMO, Error messages about C++'s templates are very meaningful. It's just that 6 pages of pure meaning are a bit much for some people.Owen wrote:Now, "Generics" may indeed be simpler - and they definitely produce much better error messages.
No one said anything about throwing out templates. Templates is a good feature but when you use them you have to look out a bit. There are things like code duplication, unnecessary copying and other things that you might want to look into. There are critical places in kernels where you might have the extra control over produced code.Owen wrote:If you just use C++ as C with classes, and throw out templates and exceptions, then you throw out much of what makes C++ nice to work with.skyking wrote:It's amazing how people get the post wrong. The suggestion was that one would be safe if you used C++ as "C with classes", and not that C++ in fact was that, and it's pretty much true. The reasons you would end up with larger or slower executable with C++ than C is that you're using features of C++ that is not in C - otherwise you should end up with the same result (at least if you turn of exception handling and RTTI in the compiler). Templates for example can easily lead to code bloat if you don't take care to avoid code duplication (which can mean that you're using memory less effectively)...pcmattman wrote: Well, unless you just stepped out a Delorean, C++ is definitely not C with classes. Who really cares what it was when it's definitely not that right here in 2010?
Calling C++ "C with classes" is a really easy way to get out of writing decent C++, or even to fight against the language. You can tear the language to pieces if all it is to you is "C with classes". When you consider the other features (in my opinion the main feature of interest is templates) you start entering territory you can't come close to handling in C. Sure, you can make a "generic" linked list implementation in C that works off void pointers... but in C++ that can be fully type-checked at compile time, and use memory more effectively, with a template.
Such as the STL. Thats right: Even if you permit use of external templates, you threw it out with exceptions.
And I find it crazy that people suggest that templates - one of the most benign C++ features - should be ignored.
And I can drive very safe if I only use my car in first gear (I won't get anywhere fast, and there's still a risk - but significantly less than if I pushed the car to its limit!).skyking wrote:It's amazing how people get the post wrong. The suggestion was that one would be safe if you used C++ as "C with classes"pcmattman wrote: Well, unless you just stepped out a Delorean, C++ is definitely not C with classes. Who really cares what it was when it's definitely not that right here in 2010?
Calling C++ "C with classes" is a really easy way to get out of writing decent C++, or even to fight against the language. You can tear the language to pieces if all it is to you is "C with classes". When you consider the other features (in my opinion the main feature of interest is templates) you start entering territory you can't come close to handling in C. Sure, you can make a "generic" linked list implementation in C that works off void pointers... but in C++ that can be fully type-checked at compile time, and use memory more effectively, with a template.
I noted you say that, but I've not seen anyone stating otherwise. Just because C++ has all kind of features does not mean that you have to use all those features in every situation, some features also require some care if you're to avoid code bloat.pcmattman wrote:And I can drive very safe if I only use my car in first gear (I won't get anywhere fast, and there's still a risk - but significantly less than if I pushed the car to its limit!).
In my opinion, you should still be using C if all you see C++ as is C with classes. Like I said - calling C++ "C with classes" is a cop-out, nothing more, nothing less, and calling it "safe" is merely an attempt to justify that statement.
Code: Select all
#ifdef __cplusplus
extern "C"{
#endif
/* Normal C header stuff */
#ifdef __cplusplus
}
#endif
I wouldn't know about the reason that isn't done, but it does dilute the air of minimalism. Although that wouldn't actually add any overhead to the binary, it would add unneeded overhead in the human sense.TylerAnon wrote:Is there some reason, that I'm missing, why this isn't widely done?
Code: Select all
class IOutputStream
{
public:
static const uint8_t tabWidth;
public:
IOutputStream();
virtual ~IOutputStream();
virtual IOutputStream& putChar(char ch) = 0;
virtual uint32_t getPosition() = 0;
virtual uint32_t setPosition(uint32_t pos) = 0;
virtual IOutputStream& print(const char* str);
virtual IOutputStream& printf(const char* fmt, ...);
};
Nice... but... printf()? Eeeewwwww....rJah wrote:For example, here is my output stream interface, which implements printf and print, relying on a platform specific putChar function.
...or putting a dent in their designing by limiting themselves to a subset and not using the tool that's best for the job.Edit: Also, as Bjarne Stroustrup once said, the good programmers know which features to use for the job at hand, the bad ones just go using everything...
Different subject: Overload operators only where it would feel natural and unambiguous to do so, and nowhere else. Oh, and don't overload || and &&. (Because you cannot emulate the "short circuit" semantics of the originals.)...like implementing custom operators for every class they have. (or something like that, cant find the source)
Which is why I don't overload << in my output stream, I always found the "shift stream left" kind of strange.Solar wrote:Overload operators only where it would feel natural and unambiguous to do so, and nowhere else,
Not sure if this is the best example for the usefulness of inheritance. You have one derived class per architecture in the source code, so what ends up in the binary (which is architecture specific) is exactly one implementation. You can certainly handle this without inheritance or any C++ - just link the the right putchar.o for the architecture. And you even save the overhead of virtual functions if you do without inheritance.rJah wrote:Platform independent stuff goes in abstract classes, which HAL classes then inherit and implement platform specific stuff. For example, here is my output stream interface, which implements printf and print, relying on a platform specific putChar function.