C++ Logo

std-proposals

Advanced search

Re: [std-proposals] c++ interfaces vs templates

From: Magnus Fromreide <magfr_at_[hidden]>
Date: Thu, 8 Dec 2022 20:44:44 +0100
On Thu, Dec 08, 2022 at 11:23:38AM -0500, Erik Aronesty via Std-Proposals wrote:
> Has anyone proposed something like this?
>
> ---
> interface sizeable {
> size_t size() cost;
> }
>
> bool too_big(sizeable thing) {
> return thing.size() > 100;
> }
>
> void main() {
> std::cout << too_big(string("sdfkjhsdfjh")) << std::endl;
> std::cout << too_big(vector<int>(1000,0)) << std::endl;
> }

This is rather similar and there is even a reference to a paper:

https://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_5.html#SEC112

That extension has been removed from gcc since then, I think it was about
the turn of the century.

/MF

> ---
>
> the precompiler can then generate specializations as needed, in the same
> way that templates generate specializations, so the resulting code can be
> as efficient as templates
>
> the advantage is that
>
> - it's just as efficient as templates
> - it's easier to teach/understand
> - avoids virtual stuff
> - avoids complex inheritance
> - works with existing libs
> - should result in much better error messages than templates

Received on 2022-12-08 19:44:48