I keep meaning to write this somewhere and failing, so here you go, this text bo...
David Chisnall (*Now with 50% more sarcasm!*) @david_chisnall@infosec.exchange
I keep meaning to write this somewhere and failing, so here you go, this text box gets it:
People periodically ask why C++ compilers can check only one half of C++ concepts at compile time. If you write a class and pass it to a template that takes a concept argument, and the class does not implement the concept, the compiler generates an error (this is the big win for concepts: you get a 'this class does not implement this concept because X' error and not an 'I tried to use this type and here is a back trace of 20 screens full of instantiation where something calls a method that doesn't exist, good luck' error). For example, where you might previously have written:
template<typename T>
class Foo
{
...
}
Now you write:
template<SomeConcept T>
class Foo
{
...
}
And if people try to write Foo<MyClass> and MyClass doesn't conform to SomeConcept, they get an error right at the point where they wrote it. This is a big improvement.
But if your template that takes a concept but calls a method that doesn't exist in the concept, that isn't caught at compile time. Why is this the case? The interface is defined by the concept, you called something that isn't in the interface, why doesn't the compiler error? These seem, at first glance, as if they're equivalent checks.
And the reason for this is closely related to the way concepts are defined. They are not just interface types. They are expressions that the type is checked against. And that's where the asymmetry comes from.
This is because concepts were designed to be able to support existing C++ patterns. C++ lets you do things like have a static constexpr bool in a class that defines whether it implements some feature, and then you can write things like:
template<typename T>
....
if constexpr (T::SupportsFoo)
{
someInstanceOfT.foo();
}
```
And you can express in the concept the idea that the type must have a `static constexpr bool SupportsFoo` field, and *if* the value of that is true, then it must provide a `foo` method that is callable with no arguments (which may be a method with no arguments, or a method where all of the arguments have default values).
All of this was possible in C++11 with `std::enable_if` and something that was roughly equivalent was possible even in C++98 with SFINAE, `static const bool` fields, and nested templates rather than `if constexpr` (the latter in C++11 made it *much* nicer). If concepts had been unable to express things that real C++ code already expresses in more ad-hoc ways, they would have been less valuable. Instead, anything that you could previously express with type-level predicates, SFINAE, and suffering, can now be cleanly expressed with concepts.
So how would you type check this, with no concrete `T`? Well, in this case it's possible. You could, for example, assume `T:: SupportsFoo` is true, and that the instance *doesn't* provide a method callable with `.foo()`, and see if that would violate the concept. And, if it doesn't, then that's fine.
But this is the absolute simplest non-trivial case. Concepts can express arbitrarily complex relationships like this, including negation (useful, for example, for saying that if `T` supports mutation then it does *not* implement a method that exposes a stable view of its internal state). And this rapidly becomes an arbitrary constraint-solving problem. In fact, you can easily define a concept that *is* any arbitrary boolean satisfiability expression: that's effectively what concepts are. And this is trivial in one direction: Given a set of candidate values, is this a solution? But it's NP-Complete in the opposite direction (does there exist a valid solution to this). And generally people don't like it when the compiler has to solve NP-complete problems over values of *n* that can easily grow as the programmer improves APIs.
I haven't dug in sufficiently far to be 100% sure, but I suspect that the fact that `requires` clauses are arbitrary expressions may mean that there are some cases that are not even computable. And people are already happy with the number of non-computable problems a C++ compiler must solve, so adding one more makes them unhappy.