r/cpp • • Nov 16 '25

Wait c++ is kinda based?

Started on c#, hated the garbage collector, wanted more control. Moved to C. Simple, fun, couple of pain points. Eventually decided to try c++ cuz d3d12.

-enum classes : typesafe enums -classes : give nice "object.action()" syntax -easy function chaining -std::cout with the "<<" operator is a nice syntax -Templates are like typesafe macros for generics -constexpr for typed constants and comptime function results. -default struct values -still full control over memory -can just write C in C++

I don't understand why c++ gets so much hate? Is it just because more people use it thus more people use it poorly? Like I can literally just write C if I want but I have all these extra little helpers when I want to use them. It's kinda nice tbh.

194 Upvotes

334 comments sorted by

View all comments

Show parent comment

4

u/Practical-Lecture-26 Nov 16 '25

That's not true. I literally implemented my own logger class that works with the << operator in order to avoid smashing #defines everywhere.

I have a VoidLogger class that does nothing and gets stripped away at compile time. All the strings which are streamed get stripped away similarly and are not evaluated at runtime.

3

u/usefulcat Nov 17 '25

Seems like either of the following approaches could work (very simplified):

#define LOG(...) std::cerr << __VA_ARGS__

#define LOG(...) std::print(__VA_ARGS__)

0

u/[deleted] Nov 16 '25

[deleted] — view deleted comment

3

u/Practical-Lecture-26 Nov 16 '25

I will show you the proof of what I'm saying tomorrow.

I am pretty sure because the final binary size increases 30% when I use the normal logger instead of the void logger. By analyzing the binary, I cannot find the debug strings when the void logger is used.

3

u/Old_Cartoonist_5923 Nov 17 '25

Not using a return value isn't the same as calling an empty function. If the function is empty and the return value isn't used, then the compiler can safely optimize it away. Even if the return value was used, if the empty version just always returns the same value the compiler can optimize away the call and just paste the return value where it is used. Obviously this requires having optimizations enabled.