Hacker News new | ask | show | jobs
by jcelerier 1452 days ago
> The goal of OOP or any programming system is to help you enforce invariants to improve correctness of a program.

enforcing invariants means reducing the number of types that satisfy the invariant. I wouldn't call doing what you ask for "enforcing" any invariant.

> Bonus: write it for any struct containing firstName and lastName.

well,

    auto concatenate(auto t) { return t.firstName + " " + t.lastName; }
satisfies your condition - here's one that'll also handle middle names: https://gcc.godbolt.org/z/YfTYs6MMn ; again I don't think anyone wants this when they say "enforcing invariants", this does exactly the opposite of what is actually wanted.

> I guess the future of C++ is that all code ever lives in headers.

or in modules, which makes it very similar than other languages with generics instantiation

1 comments

Ahh you’re, right, I forgot about fully auto’d functions. I think want more structure than just auto everything, although the compilers should find mistakes with that. Concepts will be nice.
The concept version here would look like:

    template<typename T>
    concept WesternishName = requires (T t) { 
      { t.firstName } -> convertible_to<string>; 
      { t.lastName } -> convertible_to<string>;
    };

    auto concatenate(WesternishName auto t) { return t.firstName + " " + t.lastName; }