Topic: Static polymorphism problem


Author: morwenn29@gmail.com
Date: Sat, 9 Nov 2013 06:31:21 -0800 (PST)
Raw View
------=_Part_1040_28845156.1384007481991
Content-Type: text/plain; charset=ISO-8859-1

Ever since I started using static polymorphism, I encountered a problem:
accessing the typedefs of the Derived class in the Base class.
In order not to be bothered, I use a trait class which contains the
typedefs:

    template<typename T>
    struct types_t;

    template<typename Derived>
    struct Base
    {
        using value_type = typename types_t<Derived>::value_type;
    };

    struct DerivedClass;

    template<>
    struct types_t<DerivedClass>
    {
        using value_type = int;
    };

    struct DerivedClass:
        Base<DerivedClass>
    {
        using typename Base<DerivedClass>::value_type;
    };

In one of my projects, I use static polymorphism quite often. Therefore, I
have a header containing the struct types_t<>, which is the
one that contains the subtypes for my classes using static polymorphism.
The problem is that for some generic tools defined in the
library, user have to create a specialization of this types_t<> class for
their own types, which in not really clean.

There is probably a need for some standard way to solve more cleanly this
use case. While defining a standard std::subtypes<> is
feasible, their ought to be some cleaner solution. Do you think such a tool
would be handy, and if so, do you have any idea to define
some cleaner tool?

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_1040_28845156.1384007481991
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Ever since I started using static polymorphism, I encounte=
red a problem: accessing the typedefs of the Derived class in the Base clas=
s.<br>In order not to be bothered, I use a trait class which contains the t=
ypedefs:<br><br>&nbsp;&nbsp;&nbsp; template&lt;typename T&gt;<br>&nbsp;&nbs=
p;&nbsp; struct types_t;<br>&nbsp;<br>&nbsp;&nbsp;&nbsp; template&lt;typena=
me Derived&gt;<br>&nbsp;&nbsp;&nbsp; struct Base<br>&nbsp;&nbsp;&nbsp; {<br=
>&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; using value_type =3D typename types_t&lt;=
Derived&gt;::value_type;<br>&nbsp;&nbsp;&nbsp; };<br><br>&nbsp;&nbsp;&nbsp;=
 struct DerivedClass;<br><br>&nbsp;&nbsp;&nbsp; template&lt;&gt;<br>&nbsp;&=
nbsp;&nbsp; struct types_t&lt;DerivedClass&gt;<br>&nbsp;&nbsp;&nbsp; {<br>&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using value_type =3D int;<br>&nbs=
p;&nbsp;&nbsp; };<br><br>&nbsp;&nbsp;&nbsp; struct DerivedClass:<br>&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Base&lt;DerivedClass&gt;<br>&nbsp;&nbsp=
;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using typename Base=
&lt;DerivedClass&gt;::value_type;<br>&nbsp;&nbsp;&nbsp; };<br><br>In one of=
 my projects, I use static polymorphism quite often. Therefore, I have a he=
ader containing the struct types_t&lt;&gt;, which is the<br>one that contai=
ns the subtypes for my classes using static polymorphism. The problem is th=
at for some generic tools defined in the<br>library, user have to create a =
specialization of this types_t&lt;&gt; class for their own types, which in =
not really clean.<br><br>There is probably a need for some standard way to =
solve more cleanly this use case. While defining a standard std::subtypes&l=
t;&gt; is<br>feasible, their ought to be some cleaner solution. Do you thin=
k such a tool would be handy, and if so, do you have any idea to define<br>=
some cleaner tool?<br></div>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_1040_28845156.1384007481991--

.


Author: David Krauss <potswa@gmail.com>
Date: Sun, 10 Nov 2013 09:46:06 +0800
Raw View
On 11/9/13 10:31 PM, morwenn29@gmail.com wrote:
> Ever since I started using static polymorphism, I encountered a problem:
> accessing the typedefs of the Derived class in the Base class.
> In order not to be bothered, I use a trait class which contains the
> typedefs:

Note that the problem only applies while the derived class is
incomplete. This is just a subcase of a more general issue in CRTP. You
can work around it by moving the typedefs into member definitions.

> There is probably a need for some standard way to solve more cleanly this
> use case. While defining a standard std::subtypes<> is
> feasible, their ought to be some cleaner solution. Do you think such a tool
> would be handy, and if so, do you have any idea to define
> some cleaner tool?

You can leverage ADL to link the traits class to the implementation
class without explicit specialization. See my recent StackOverflow post
http://stackoverflow.com/questions/19851629/inheriting-or-member-traits-idiom
..

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

.