C++ Logo

std-proposals

Advanced search

Re: New "static_virtual" keyword

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Wed, 17 Mar 2021 20:19:32 -0400
On Wed, Mar 17, 2021 at 3:41 PM Phil Bouchard via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> New keyword proposal to allow inheritance at compile-time. Ex:
>
> template <typename T>
>
> struct container
>
> {
>
> static_virtual T * begin() {...}
>
> static_virtual T * end() {...}
>
> static_virtual T * rbegin() {...}
>
> static_virtual T * rend() {...}
>
> };
>
> template <typename T>
>
> struct list : container<T>
>
> {
>
> static_virtual T * begin() {...}
>
> static_virtual T * end() {...}
>
> static_virtual T * rbegin() {...}
>
> static_virtual T * rend() {...}
>
> };
>
> template <typename T>
>
> ostream & ostream(ostream & out, container<T> const & c)
>
> {
>
> for (auto i = c.begin(); i != c.end(); ++ i)
>
> out << * i << endl;
>
> return out;
>
> }
>
> int main()
>
> {
>
> list<int> l;
>
> l.push_back(1);
>
> l.push_back(2);
>
> l.push_back(3);
>
> cout << l << endl;
>
> }
>
>
> Regards,

I'm not sure I understand the point of this. Or rather, I don't
understand the point of this as distinct from the point of just having
a concept and a template:

```
template<typename C>
concept Container = requires(C c)
{
  {c.begin()} -> is_pointer_type;
  {c.end()} -> is_pointer_type;
  {c.end()} -> std::is_same<decltype(c.begin())>;
  //etc.
};

template<Container C>
auto &operator<<(std::ostream&, C const&c);
```

If there is no vtable, and the lookup is all done at compile-time,
then I see no need to restrict everything to such a coarse mechanism
as overriding specific functions by signature. Concepts are the
compile-time way to define statically-polymorphic prototypes, and
concept-checking is how we declare that a function takes a
statically-polymorphic prototype.

Received on 2021-03-17 19:19:46