"Jason House" wrote > Andrei Alexandrescu wrote: > >> I'd be curious to find out more about a runtime queryable struct >> interface. How would it work? What idioms would it enable? > > I don't know what Lars is thinking of, but I think of struct interfaces as > a non-polymorphic / compile-time inheritance. AKA, you can have a struct > implement it, but you can't cast it to a base struct/interface. Outside > of defining the struct, I'd expect it to only be usable in templates and > is-expressions
I would expect a struct interface to be castable to the interface. Otherwise, you get no benefit from having it. You can already use compile-time interfaces with concepts and templates. The issue then becomes to make a struct interface act the same as a class interface. Unfortunately, a class interface value only contains a vtable pointer (I think), which contains the offset to get to the actual class pointer. If the class pointer was passed with the interface, this would be much easier, but might be slower when doing class interfaces (passing 2 pointers instead of 1). You could put an interface vtable pointer at the beginning of a struct, which then makes every struct carry that extra baggage. And then a virtual call is required, but would allow much more dynamic behavior. You could build a temporary interface "structure" which does the thunk to point to the real struct, since you don't have to deal with possible inheritance, to avoid putting a vtable in the struct. There are probably other ways. In any case, having struct interfaces probably is not the best method for ranges, where you would be doing a virtual call over and over again. But it would be hugely beneficial for things like non-templated I/O, or serialization. One problem to consider is that it is much easier for a struct to escape it's scope, so you may have to require a scope parameter to pass a struct as an interface. -Steve
