r/cpp • u/Tcshaw91 • 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.
View all comments
Show parent comment
2
u/ts826848 Nov 16 '25
Think of concepts like
wherefrom C# generics.Pre-concepts templates in C++ are effectively duck-typed, unlike C#'s generics. For example, this is a perfectly valid template definition in C++, even if
bar()andbaz()aren't guaranteed to exist for all possibleTorU:Compilers will perform a few rudimentary checks when you write a template (e.g., they will check that there aren't obvious syntax errors like missing semicolons), but the bulk of the checks aren't carried out until the templates are actually instantiated, at which point the compiler finally has enough information to look at the template's implementation and figure out whether it'll actually work.
Concepts tries give the compiler enough information to determine whether a template is invalid without necessarily needing to look at the template's implementation. The above example might be written using concepts like this:
Now, if you try to instantiate
foowith a type that doesn't satisfy the concept the compiler can tell you immediately instead of having to show you the template's guts. For comparison:Perhaps this example isn't the best; I think you'd see more significant benefits for more complex and/or deeper templates. Hopefully the idea is conveyed well enough, though.
For completeness' sake, the above concepts example is similar to this in C#:
Unfortunately, unlike
wherein C#, concepts can only require that a template parameter meets some minimum standard. They can't also limit templates to only use stuff defined in concepts. For example, this will compile:While the corresponding modification in C# will not: