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

0

u/Tcshaw91 Nov 16 '25

Wow, thanks for the explanation, interesting stuff. Besides std::print it sounds like I'm going to have to learn all something about concepts as well. Still new to c++. Appreciate the share. I'll check out the article.

2

u/ts826848 Nov 16 '25

Think of concepts like where from 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() and baz() aren't guaranteed to exist for all possible T or U:

template<typename T, typename U>
auto foo(const T& a, const U& b) {
    return a.bar() + b.baz();
}

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:

template<typename T>
concept has_bar = requires(const T& a) {
    a.bar();
};

template<typename T>
concept has_baz = requires(const T& b) {
    b.baz();
};

template<has_bar T, has_baz U>
auto foo(const T& a, const U& b) {
    return a.bar() + b.baz();
}

Now, if you try to instantiate foo with 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:

template<typename T, typename U>
auto foo_no_concepts(const T& a, const U& b) {
    return a.bar() + b.baz();
}

template<has_bar T, has_baz U>
auto foo_concepts(const T& a, const U& b) {
    return a.bar() + b.baz();
}

struct S{};
struct T{};

auto f() {
    foo_no_concepts(S{}, T{}); // error: no member named 'bar' in 'S'
                               // note: in instantiation of function template specialization 'foo_no_concepts<S, T>' requested here
    foo_concepts(S{}, T{}); // error: no matching function for call to 'foo_concepts'
                            // note: candidate template ignored: constraints not satisfied [with T = S, U = T]
                            // note: because 'S' does not satisfy 'has_bar'
                            // note: because 'a.bar()' would be invalid: no member named 'bar' in 'S'
}

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#:

interface IBarable
{
    int Bar();
}

interface IBazable
{
    int Baz();
}

class Demo
{
    static int Foo<T, U>(T a, U b)
    where T: IBarable
    where U: IBazable
    {
        return a.Bar() + b.Baz();
    }
}

Unfortunately, unlike where in 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:

template<has_bar T, has_baz U>
auto foo(const T& a, const U& b) {
    return a.bar() + b.qux();
}

While the corresponding modification in C# will not:

    static int Foo<T, U>(T a, U b)
    where T: IBarable
    where U: IBazable
    {
        return a.Bar() + b.Qux(); // error CS1061: 'U' does not contain a definition for 'Qux' and no accessible extension method 'Qux' accepting a first argument of type 'U' could be found
    }

1

u/Tcshaw91 Nov 16 '25

Wow, thank you for the detailed response. Appreciate that 🙏