Defining interfaces in C++ with ‘concepts’ (C++20)(lemire.me)
lemire.me
Defining interfaces in C++ with ‘concepts’ (C++20)
https://lemire.me/blog/2023/04/18/defining-interfaces-in-c-with-concepts-c20/
76 comments
does this imply that type erasure via base classes will become a thing of past ?
concepts don't change codegen in any way, they just mean your error messages become actually sane :D
Essentially, in the past you would have your template code, say something dumb like:
Unfortunately this does not stop you writing a template that depends on things that your concepts don't guarantee. For example, if we can halve something, we must be able to double it, right? (silly example to demonstrate the issue)
Now we can do `double_it(1)` and that will work, but `double_it("foo")` will say the error is at the t * 2 in the body, when we probably want the error to actually at the point we try to call double_it.
Preventing this kind of error is non-trivial (possibly actually impossible?) given how concepts are defined. It's literally just a list of statements and expressions that need to be valid for a type. But going from a list of "these statements and expressions are valid" to "is this specific expression or statement valid in a template" is at best nontrivial. This is core limitation of the entire feature.
Essentially, in the past you would have your template code, say something dumb like:
template <typename T> T halve(T t) { return t / 2; }
and if you instantiated with the wrong type, say: halve("foo")
you get an error message pointing to the t/2 in the halve implementation. As your templates become less trivial, so do the error messages. So we could apply concepts: template <typename T> concept Halvable = requires (T t) { t / 2; };
template <Halvable T> T halve(T t) { return t / 2; }
Now our call to halve("foo") will complain that const char* is not Halvable. It will also produce an error similar to the original saying that we don't conform to Halvable because the `t / 2` expression fails. In this case it's not super valuable, but if you were instantiating a type or method that was more complicated instead of getting dozens or hundreds of errors in the instantiated body of the template, you just get an early error saying "you aren't conforming to X for these reasons:...".Unfortunately this does not stop you writing a template that depends on things that your concepts don't guarantee. For example, if we can halve something, we must be able to double it, right? (silly example to demonstrate the issue)
template <Halvable T> T double_it(T t) { return t * 2; }
Note, Halvable doesn't ensure that * is available, but this template is still "correct".Now we can do `double_it(1)` and that will work, but `double_it("foo")` will say the error is at the t * 2 in the body, when we probably want the error to actually at the point we try to call double_it.
Preventing this kind of error is non-trivial (possibly actually impossible?) given how concepts are defined. It's literally just a list of statements and expressions that need to be valid for a type. But going from a list of "these statements and expressions are valid" to "is this specific expression or statement valid in a template" is at best nontrivial. This is core limitation of the entire feature.
Arguably, pointing to t/2 (in the second case) is the correct behaviour, because (if concepts are being used), then it is the function (double_it) that is mis-specifying it’s requirements. Even better would be to point at the function definition as well and say that there is nothing in the concept that allows this function.
I think the core problem (don’t know if this is still the case, haven’t actually used concepts) is that templates raise errors at the point of expansion, rather than at the point of definition, because of how they are specified/implemented.
I think it’s possible that the compiler could do something smart by auto-creating a temp type that is the minimal possible implementation of the concept and attempting to compile the function. Any errors that result, should be flagged at the concept specified in the function.
I think the core problem (don’t know if this is still the case, haven’t actually used concepts) is that templates raise errors at the point of expansion, rather than at the point of definition, because of how they are specified/implemented.
I think it’s possible that the compiler could do something smart by auto-creating a temp type that is the minimal possible implementation of the concept and attempting to compile the function. Any errors that result, should be flagged at the concept specified in the function.
> In Go, I found that using an interface was not free: it can make the code slower. In C++, if you implement the following type and call count on it, you find that optimizing compilers are able to just figure out that they need to return the size of the inner vector.
I'm surprised at this, do Go interfaces really introduce much overhead? Of course this depends on the level of performance you care about but surely, being a statically typed language, lots of the same optimisations are available.
I'm surprised at this, do Go interfaces really introduce much overhead? Of course this depends on the level of performance you care about but surely, being a statically typed language, lots of the same optimisations are available.
Interface methods in Go are like virtual methods in C++. In principle C++ compilers when statically compiling everything can often remove virtual method indirection for some objects, but I think in general this optimization is still uncommon and difficult to coax out a compiler. Go definitely does not do this, even though in principle it should be easier.
Basically, "interfaces" in Go and C++ actually refer to quite different language features. (Or at least, the author is using the term to describe quite different language features.)
Basically, "interfaces" in Go and C++ actually refer to quite different language features. (Or at least, the author is using the term to describe quite different language features.)
This is common when doing LTO, without it there is no guarantee that there isn't some dynamically loaded code that would be broken, this is one area where JIT focused languages have an advantage.
Indeed you need LTO for generalized devirtualization, but guarded devirtualization, static classes and final can still help even without LTO.
There is overhead in interface indirection because the Go runtime needs to perform dynamic dispatch to determine which method to call at runtime.
C++ can optimize interface indirection away because it supports static polymorphism, which allows the compiler to generate specialized code for each concrete type used with a generic interface, eliminating the need for dynamic dispatch.
C++ can optimize interface indirection away because it supports static polymorphism, which allows the compiler to generate specialized code for each concrete type used with a generic interface, eliminating the need for dynamic dispatch.
Go generally is pretty conservative about that kind of thing (namely, compiler optimizations). Go generally abides by a “what you write is what you get” kind of thing, especially when it comes to “non-local” optimizations. It’s generally opposed to anything that’s “clever.” (Just my feeling as someone who uses Go pretty often and who respects the choice they’ve made on that spectrum).
[deleted]
Interfaces always go to the heap in go, so yes, you can take some simple code that could easily live on the stack and make it slower by wrapping it in interfaces with go.
As always, very informative and a perfect "snippet" size to quickly read and learn a new thing or two. Thanks!
Meta: I think the very first sentence is the victim of some drive-by editing, and needs one more pass. I'm not a native speaker, but I still suggest changing
In an earlier blog post, I showed on the Go programming language allow you to write generic functions once you have defined an interface.
Into perhaps
In an earlier blog post, I showed how the Go programming language allows you to write generic functions once you have defined an interface.
Considering the audience and author, I would seriously consider omitting the explanation of what Go is, but that's just polish. :)
Meta: I think the very first sentence is the victim of some drive-by editing, and needs one more pass. I'm not a native speaker, but I still suggest changing
In an earlier blog post, I showed on the Go programming language allow you to write generic functions once you have defined an interface.
Into perhaps
In an earlier blog post, I showed how the Go programming language allows you to write generic functions once you have defined an interface.
Considering the audience and author, I would seriously consider omitting the explanation of what Go is, but that's just polish. :)
There are quite a few little language mistakes that I couldn't figure out if it was a language thing or just a typo.
> Of course, it also limits to the tools that I use to program: they cannot much about the type I am going to have in practice within the count function.
I think dropping the 'me' is often a feature of those whose native language is eastern European/Russian. The second (possibly) missing 'know' seems to support just editing mistakes. The structure of both makes me think of Portuguese for some reason!
To avoid going off topic on HN, something... something... ChatGPT
> Of course, it also limits to the tools that I use to program: they cannot much about the type I am going to have in practice within the count function.
I think dropping the 'me' is often a feature of those whose native language is eastern European/Russian. The second (possibly) missing 'know' seems to support just editing mistakes. The structure of both makes me think of Portuguese for some reason!
To avoid going off topic on HN, something... something... ChatGPT
I would argue Java interfaces are very different from Go interfaces and C++ concepts, because the former is nominally typed and the latter are structural.
That's something I have been pondering for some time.
I believe it's a false dichotomy.
My thought is still that structural supersedes nominal.
A nominal interface is just another constraint added to the list of constraints of an underlying structural interface?
I believe it's a false dichotomy.
My thought is still that structural supersedes nominal.
A nominal interface is just another constraint added to the list of constraints of an underlying structural interface?
In a nominal type system, a method x() is part of the interface X, while in a structural one it's part of the implementor of said interface. In Go there's a Human.HasOrgan(), not an AbstractBody.HasOrgan().
A consequence of this is that in Rust, which has a nominal system, you can implement two traits that contain a method with the same name and are required to disambiguate at the call site. In Go you can't do that, since the method is part of the concrete type.
A consequence of this is that in Rust, which has a nominal system, you can implement two traits that contain a method with the same name and are required to disambiguate at the call site. In Go you can't do that, since the method is part of the concrete type.
Fair. That's not really in contradiction too.
The additional naming constraint added to a structural interface would form a sort of namespace for methods.
I think in the comments below that someone likens this to tags in C++.
The additional naming constraint added to a structural interface would form a sort of namespace for methods.
I think in the comments below that someone likens this to tags in C++.
A nominal type system is not a more constrained version of a structural one. That statement would imply that any program written for the former would work using the latter as well, which is false. Name collisions would simply not resolve.
For it to work, you need to add a namespace to all the colliding methods (a simple one would be a prefix like people do in C).
A nominal system is a more constrained structural system in some ways, but the opposite is true as well, so it's not as simple as 'nominal is subset of structural'.
For it to work, you need to add a namespace to all the colliding methods (a simple one would be a prefix like people do in C).
A nominal system is a more constrained structural system in some ways, but the opposite is true as well, so it's not as simple as 'nominal is subset of structural'.
Hmmh. You seem to be restating what was said above.
A nominal type system still is superseded by a structural type system.
The difference is in how a type is defined. Or what kind of constraints are in entailment said otherwise.
An interface enforces constraints. The difference here is merely that the current implementations only have either one of these type of interfaces. So for the structural type system, all methods are in the global namespace, somehow.
That's all. Because our current languages are this way doesn't mean that the two concepts cannot be reconciliated or that one is just better than the other.
A nominal type system still is superseded by a structural type system.
The difference is in how a type is defined. Or what kind of constraints are in entailment said otherwise.
An interface enforces constraints. The difference here is merely that the current implementations only have either one of these type of interfaces. So for the structural type system, all methods are in the global namespace, somehow.
That's all. Because our current languages are this way doesn't mean that the two concepts cannot be reconciliated or that one is just better than the other.
Yeah I think our arguments overlap in some ways.
> That's all. Because our current languages are this way doesn't mean that the two concepts cannot be reconciliated or that one is just better than the other.
I don't think I agree with this though, I believe they're fundamentally different. The whole point of structural constraints is that they don't need the type to be aware of them. The point of nominal constraints though is that they require the type to explicitly acknowledge them.
In an ideal situation, everyone names and types things the same ('logical') way, so structural constraints 'just work'. A type implements has_organ, and an interface requires has_organ, and the type is automatically compatible with the interface.
A nominal system is the opposite though; the type explicitly understands what a specific interface implies and formally states it.
I just can't see how there's a subset-superset relationship, or how they can somehow be reconciled.
> That's all. Because our current languages are this way doesn't mean that the two concepts cannot be reconciliated or that one is just better than the other.
I don't think I agree with this though, I believe they're fundamentally different. The whole point of structural constraints is that they don't need the type to be aware of them. The point of nominal constraints though is that they require the type to explicitly acknowledge them.
In an ideal situation, everyone names and types things the same ('logical') way, so structural constraints 'just work'. A type implements has_organ, and an interface requires has_organ, and the type is automatically compatible with the interface.
A nominal system is the opposite though; the type explicitly understands what a specific interface implies and formally states it.
I just can't see how there's a subset-superset relationship, or how they can somehow be reconciled.
One way to see it is that a type has a given methods located in a given namespace in the nominal type system.
A nominal type system doesn't necessarily enforce semantics either.
It just enforces the location of a method definition.
Seen that way, because the relation is dual, one could indeed claim that a structural interface is a nominal interface where the name constraint is elided.
But just as in subtyping, one less constraint also means bigger set.
Of course if one were to decide that an object satisfying a nominal interface doesn't satisfy the structural interface obtained by ignoring the namespace, then I'd agree as well, these concepts would be disjoint.
I don't think they are though but I don't know of a language that ever mixed both either.
A nominal type system doesn't necessarily enforce semantics either.
It just enforces the location of a method definition.
Seen that way, because the relation is dual, one could indeed claim that a structural interface is a nominal interface where the name constraint is elided.
But just as in subtyping, one less constraint also means bigger set.
Of course if one were to decide that an object satisfying a nominal interface doesn't satisfy the structural interface obtained by ignoring the namespace, then I'd agree as well, these concepts would be disjoint.
I don't think they are though but I don't know of a language that ever mixed both either.
AFAIK Python [optional] type system supports both.
The nominal types are the "common" types, while the protocols [1] are structural. It's quite cool, actually :)
[1] https://peps.python.org/pep-0544/
[1] https://peps.python.org/pep-0544/
Nominal interfaces can still be useful though, as they convey a stronger sense of intent than structural. For example, java.io.Serializable is a completely empty interface that classes “implement” to signal that they are safe to serialize. As a structural interface, it’d be useless.
structurally you can do something similar by adding some tag (in the form of a constant or nested type) to your class. For example:
template<class T> concept serializable = requires { typename T::is_serializable_tag; };
void serialize(serializable auto x) {...};
struct NotSerializableClass { ... };
struct SerializableClass { using is_serializable_tag = void; ... };
serialize(NotSerializableClass{}); // error
serialize(SerializableClass{}); // all good
In C++, specializing a trait is also an option. So, while nominal and structural interfaces are not the same, sometimes the lines are blurred.Well it depends on a runtime/compile-time distinction. A nominal type is a structural type with a compile-time constraint.
If you have compile-time only constants you can model nominals with structural,
If you have compile-time only constants you can model nominals with structural,
type Square
static const IsSquare = true
var length = 10
You can kinda hack-in subtyping, type Shape
static const Shape = true
type Square
import static from Shape
static const IsSquare = true
var length = 10This is routinely done in c++ with tags (for example iterator_tag). Tags inheritance is also a thing.
In practice C++ Concepts don't do what you're suggesting.
The C++ 20 Standard Library provides numerous concepts which have a very different semantic requirement than the syntax they're checking. If you violate the syntactic requirement of course that'll earn you a compiler error, but if you violate the semantic requirements that's silently an ill-formed C++ program, it has no meaning whatsoever and might do absolutely anything if run.
If these were nominal, we could say, well, nobody should have deliberately implemented this inappropriate Concept, similar to an unsafe Rust trait, the act of implementation is a promise to others. But C++ Concepts aren't nominal and so there was no opportunity to do that and so in practice such deviations are likely very common despite the potentially drastic consequences.
The C++ 20 Standard Library provides numerous concepts which have a very different semantic requirement than the syntax they're checking. If you violate the syntactic requirement of course that'll earn you a compiler error, but if you violate the semantic requirements that's silently an ill-formed C++ program, it has no meaning whatsoever and might do absolutely anything if run.
If these were nominal, we could say, well, nobody should have deliberately implemented this inappropriate Concept, similar to an unsafe Rust trait, the act of implementation is a promise to others. But C++ Concepts aren't nominal and so there was no opportunity to do that and so in practice such deviations are likely very common despite the potentially drastic consequences.
I have been programming in C++ for almost 20 years [1] and I don't remember ever being bitten by accidental concept conformance. So I object to the "likely very common" description. Implicit conformance was very much an explicit design goal.
[1] yes, concepts as an explicit language feature are new, but C++ has had de-facto concepts since Stepanov work on the original STL in the 90s.
[1] yes, concepts as an explicit language feature are new, but C++ has had de-facto concepts since Stepanov work on the original STL in the 90s.
Since I know better than to suggests C++ programmers might be more capable of making mistakes than they realise, lets try a different question: How do you spot this mistake when reviewing other people's code? Do you memorise a list of all the semantic requirements of each concept so that you can mentally check that the concept's requirements are satisfied appropriately by what was written each time ?
This isn't something I look for in code reviews because it's just not something I've ever see be the source of a bug. There are a million bugs that I've eventually tracked down to some subtle C++ thing, but I've never had one come down to a type which appears to conform to one of the standard library's concepts but actually doesn't.
I expect other people to write tests (including compile time tests).
If you were worried about behavioural problems, including UB, tests would help.
But alas the problem here is IFNDR [Ill-formed No Diagnostic Required] so the compiler can't help you. All semantic constraints are your problem as the programmer, C++ decided that it's not the compiler's concern whether the program meets semantic constraints. Testing doesn't necessarily help at all, which is probably surprising.
But alas the problem here is IFNDR [Ill-formed No Diagnostic Required] so the compiler can't help you. All semantic constraints are your problem as the programmer, C++ decided that it's not the compiler's concern whether the program meets semantic constraints. Testing doesn't necessarily help at all, which is probably surprising.
Uptrenda(5)
Really good video by Conor Hoekstra on the differences between C++ Concepts vs. Haskell Typeclasses vs. Rust Traits vs. Swift Protocols.
https://youtu.be/E-2y1qHQvTg
It is a good intro overview to the subject. Unfortunately the video has far too much filler, so you need to skip a bit e.g. start at 22:30 when the video gets into examples.
https://youtu.be/E-2y1qHQvTg
It is a good intro overview to the subject. Unfortunately the video has far too much filler, so you need to skip a bit e.g. start at 22:30 when the video gets into examples.
> So what are concept good for? I think it is mostly about documenting your code.
Emphasis mine. While it is somewhat good as documentation, sometimes static_assert with a custom diagnostic message is sometimes better for this purpose.
The hidden power of concepts/constraints is the way it shapes overload sets. You can have something like:
Emphasis mine. While it is somewhat good as documentation, sometimes static_assert with a custom diagnostic message is sometimes better for this purpose.
The hidden power of concepts/constraints is the way it shapes overload sets. You can have something like:
template <forward_range R>
void foo(R&& r) {
/* some generic algorithm */
}
template <random_access_range R>
void foo(R&& r) {
/* optimized for random access */
}
and it will work if you pass a random access range, as the compiler can deduce that it's more specific than a forward range to resolve the ambiguity. Prior to concepts writing code that did this was way more cumbersome.Indeed. I haven't found concepts to be very good at generating error messages. But they are great for documentation and to get rid of SFINAE hacks for overloading.
The shorter template syntax is a bonus.
edit: concepts should also allow for better IDE tooling, for example proper completion inside template functions; although it is supposed to work, I didn't notice it firing yet in clangd.
The shorter template syntax is a bonus.
edit: concepts should also allow for better IDE tooling, for example proper completion inside template functions; although it is supposed to work, I didn't notice it firing yet in clangd.
Rust requires trait bounds (its equivalent of concepts) to type-check the template code at definition site, when it's written, not at the instantiation site where it's used.
This results in much better error messages. The downside is that trait bounds for numeric code are awfully verbose, and you can't sneak in a printf without declaring it as a requirement.
> In Go, I found that using an interface was not free: it can make the code slower.
The Go version that was presented isn't equivalent though. In Go you are accepting an interface directly which will hide the value under some fat pointer for dynamic dispatch, in c++ you are using generics to monomorphise the function to specific types. If you want to compare the implementations fairly you should've used Go generics:
The Go version that was presented isn't equivalent though. In Go you are accepting an interface directly which will hide the value under some fat pointer for dynamic dispatch, in c++ you are using generics to monomorphise the function to specific types. If you want to compare the implementations fairly you should've used Go generics:
func Count[T IntIterable](i T) (count int) {Fair criticism, though I do wonder if it'd really make that much of a difference. Go doesn't really monomorphize generics either, and would end up with an equally if not more expensive lookup for the correct generic function at runtime.
Some reading: https://github.com/golang/proposal/blob/master/design/generi... https://planetscale.com/blog/generics-can-make-your-go-code-...
Some reading: https://github.com/golang/proposal/blob/master/design/generi... https://planetscale.com/blog/generics-can-make-your-go-code-...
I don't know why I thought Go generics also do monomorphization, must've misremembered or it was an earlier proposal? Thanks for the correction!
That's true at the moment, but still an implementation detail. I think I remember early versions of C++ compilers doing the same thing with templates.
Considering the progress Go compiler has gone through, I think it's reasonable to expect the optimized implementations will come few versions down the road.
Considering the progress Go compiler has gone through, I think it's reasonable to expect the optimized implementations will come few versions down the road.
C++ templates have never used runtime dispatch
I assume you've checked the version control history of every C++ compiler in existence?
Not the OP, however I have programmed in C++ since 1987 across many different operating systems and hardware platforms and I've literally never heard of a compiler that implements template stuff using runtime dispatch. CFront3 which was I think the first real template implementation that most people used certainly never did it that way, neither did any version of gcc, visual studio or Sun Workshop, which are the compliers I used the most from that period. Dug out my old copy of Coplien[1] which is from the early 90s and it discusses runtime dispatch in depth in the context of vtables and virtual function pointers and the cost of these things, so the concept was well understood but not a cost anyone was paying with templates.
[1] https://archive.org/details/advancedcbsprogr00copl "Advanced C++ Programming Styles and Idioms" aka the first programming book that genuinely kicked my ass when I first read it and made me realise how good it was possible to be at computer science.
[1] https://archive.org/details/advancedcbsprogr00copl "Advanced C++ Programming Styles and Idioms" aka the first programming book that genuinely kicked my ass when I first read it and made me realise how good it was possible to be at computer science.
It would be extremely hard to implement templates with dynamic dispatch while maintaining the correct semantics.
Right. For starters, from the very beginning C++ has supported function templates which take native types. So you don't even necessarily have any kind of pointer you could add a vtable to even if you wanted to. Then add to that the guarantee[1] about pod types being directly compatible with C which as you say I don't see how it owuld be possible to do.
[1] which has always been strong even before there was an actual ISO/ANSI standard
[1] which has always been strong even before there was an actual ISO/ANSI standard
templates don’t exist after the front end. there is no ABI that allows them to exist in any object file. there is no object file format they could be embedded in, sans a string representation of the source they came from.
extremely hard is underselling it somewhat :)
extremely hard is underselling it somewhat :)
I have used C++ before templates were a thing, and never ever saw one that did otherwise, added C++ to my toolbox in 1993.
uint32_t next() { index++; return array[index - 1]; }
I do like uint32_t next() { return array[index++]; }
, shame it's kinda unintuitive.