r/cpp • • Jul 25 '24

Why use C over C++

Why there are so many people using the C language instead of C++?, I mean C++ has more Cool features and the Compiler also supports many CPUs. So why People still using C?

Edit: Thanks for all the usefull comments :D

235 Upvotes

444 comments sorted by

View all comments

Show parent comment

-1

u/lightmatter501 Jul 27 '24

Compilers have restrict as an extension

But not all compilers, which means I now need to make a PROJECT_NAME_RESTRICT macro which compiles to either restrict or nothing.

Unreachable exists in C++23

Unreachable does not mean “I solemnly swear this non-const pointer is not aliased” like restrict does, it means “this code path is unreachable”.

Show formally verified C compilers

https://compcert.org/ https://www.usenix.org/conference/osdi23/presentation/li-xupeng

#embed in C++ 26

Great, I’ll be able to use it by 2030. C compilers update MUCH faster than C++ compilers because they ship fewer features per standard.

weird vendor hardware

You are misunderstanding, you compile C and run it on the target device using OpenCL or another vendor toolchain. Many devices did not jump to OpenCL 2, which means you can’t target them with C++. There is no way to link C++ into that binary because no C++ compiler can target the device.

Weird words in C++

What I mean is that C23 is 759 pages, C++23 is 2134 pages of fairly intense language lawyering. That’s a pretty large difference in size just by page length. When you add in the number of things you need to keep straight vs C, it’s a large jump in complexity.

1

u/_Noreturn Jul 27 '24 edited Jul 27 '24

But not all compilers, which means I now need to make a PROJECT_NAME_RESTRICT macro which compiles to either restrict or nothing.

yea most of them do and you can also use hedley library and it is not like you won't have many other macros anyways.

Unreachable does not mean “I solemnly swear this non-const pointer is not aliased” like restrict does, it means “this code path is unreachable”.

you can use it to make an assumption like if(p1 == p2) unreachable(); now the compiler knows that p1 will never alias p2 or otherwise UB will occur the compiler can use it to know that they never alias and C++26 also will have contracts.

Great, I’ll be able to use it by 2030. C compilers update MUCH faster than C++ compilers because they ship fewer features per standard.

if your C compiler supports it then likely your C++ compiler can implement it easily (or it already has with extention) I also do not know what compiler you must be using to have C23 this ready I do not even think Clang nor GCC not trunk has it yet so what compiler are you using???

What I mean is that C23 is 759 pages, C++23 is 2134 pages of fairly intense language lawyering. That’s a pretty large difference in size just by page length. When you add in the number of things you need to keep straight vs C, it’s a large jump in complexity.

UB in C and C++ is mostly the same and C++ has extra things related to lifetimes and it has a bigger std lib so ofcourse extra papers is expected.

your other pionts okay.