H. S. Teoh:

What if templates were modified so that assumptions about T have to be stated up front? We don't necessarily have to introduce typeclasses as a separate thing, but we could have the compiler reject template bodies that try to access internals of T that aren't stated up front. E.g.:

        auto myFunc(T)(T t) {
return t.fun(); // error: T wasn't stated to have a member named .fun
        }

would produce a compile error, whereas:

        auto myFunc(T)(T t)
                if (hasMember!(T, "fun"))
        {
return t.fun(); // OK: we stated that we expect T to have a .fun member
        }

would be accepted. The stated assumptions can themselves be templates too, which are expanded by the compiler before checking the template body. (Presumably, hasMember will eventually expand to some compiler intrinsic that tells the compiler what assumptions are being made about T.) So we can still continue using things like isInputRange!T, etc.,
except that now the compiler will catch unstated assumptions.

What's the difference between this and Concepts (lite)? :-)

Bye,
bearophile

Reply via email to