C++ Logo


Advanced search

Re: [std-proposals] Generic code, not the API you are looking for

From: Zhihao Yuan <zy_at_[hidden]>
Date: Sat, 20 Aug 2022 04:36:38 +0000
On Friday, August 19th, 2022 at 12:27 PM, Jason McKesson via Std-Proposals <std-proposals_at_[hidden]> wrote:


> First, I'm decidedly unsure about what... any of that actually is.
> What is `String S` here?

Declare a parameterized type S that satisfies String.

> Is `String` a type concept?

A protocol, just like Swift's protocol.


> I'm guessing that you probably meant something like:

> ```
> generic<String S>
> void function(S s) {
> using char_t = S::char_type;
> // ...
> if(s.starts_with(something))
> { ... }
> }
> ```



> Does this `generic` declaration mean that if I have some type `T`
> which happens to satisfy the concept `String`, there will
> automatically be a function named `function` with that implementation?

You need to use `override` to claim
that your type satisfies `String`
rather than "happens to satisfy."


> Does the `override` declaration mean that `vector<char>::starts_with`
> is a legitimate expression?


> Isn't that highly dangerous, as it
> effectively allows you to modify a class's interface without actually
> being the owner/maintainer of that type?

No either. It is outside class. It's
an entry of the protocol rather than
the type. The only way to use it is
through a parameterized type.


> Injecting definitions into a class without that class's explicit
> knowledge and consent should be a non-starter.

Doesn't apply. Never needed.

Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.

Received on 2022-08-20 04:36:43