C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Adding deduced "this" to already-existing CRTP interfaces

From: Gergely Nagy <gergely.nagy.alt_at_[hidden]>
Date: Sat, 19 Mar 2022 03:05:59 +0100
Hey,

I was thinking about similar functionalities. There could be two
interesting things to be added:


   - Allowing the use of the class name just as inside the body without
   template parameters:

   template<...stuff...>
   class CrtpActual : public CrtpBase<CrtpActual> {...};
                                               ^^^
                                               No arg list, just like in
   class body


   - Also a *baseclass*[<n>] pseudotype , that would give back the <n>th
   base class, because in case of inheritance from an ugly base template often
   requires boilerplate, like:

   template<TARGS1>
   class SomeDerived : public SomeBase<TARGS2> {
      using Base = SomeBase<TARGS2>; // Redundant

   instead of this, one could just:

   template<TARGS1>
   class SomeDerived : public SomeBase<TARGS2> {
      using Base = *baseclass*;

But I'm not sure if these are actually feasible.

Thanks,
   Gergely

Keenan Horrigan via Std-Proposals <std-proposals_at_[hidden]> ezt írta
(időpont: 2022. márc. 18., P, 19:03):

> Hi, I was wondering if it would make sense to add deduced "this" to
> already-existing CRTP interfaces, such as std::ranges::view_interface. I
> was thinking for instance it could be changed to
>
> template<typename Derived = void>
> struct view_interface {
> /* ... */
> };
>
> where for all now-existing uses of view_interface it would function as
> normal, with the inheriting class specified in the template instantiation,
> but new classes could do e.g.
>
> template</* Potentially several lengthy template parameters */>
> struct my_view : std::ranges::view_interface<> {
> /* ... */
> };
>
> which would instantiate std::ranges::view_interface<void> which would take
> advantage of the new deduced "this" pattern, cutting down on code
> duplication and over-verbosity.
>
> I don't think this would be an API-breaking change, and I'm pretty sure
> this wouldn't be ABI-breaking either since no one should have instantiated
> a std::ranges::view_interface<void>, but even if they did then they
> wouldn't be able to really do anything with it.
>
> This change though would increase the number of views with a shared parent
> class (std::ranges::view_interface<void>) which might stop space
> optimizations because two objects of the same type can't share the same
> space, even with [[no_unique_address]] (at least as I understand it). I
> personally don't find that to be a compelling counterargument though,
> especially with std::ranges::view_base already existing.
>
> But then there's also that the deduced "this" pattern could deduce a class
> that derives from "my_view" from the earlier example, which seems like a
> thing to keep in mind for any instance of using deduced "this" in a CRTP
> way, but still the question remains of is that a problem, and if so, is it
> enough to outweigh the benefits of this change? I personally am not sure.
>
> I would love to hear others' input on this idea, thank you in advance.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2022-03-19 02:06:11