why are type aliases not resolved during crtp expansion?

111 views
Skip to first unread message

Matt Langston

unread,
Sep 12, 2016, 11:10:31 AM9/12/16
to ISO C++ Standard - Discussion
I wanted my crtp base class to use a type alias defined in my derived class, but it does not work:

template <typename T>
class Base {
public:
  using int_t = typename T::int_t; // error: no type named 'int_t' in 'Derived'
};

class Derived : public Base<Derived> {
public:
  using int_t = int;
};

This seemed like a very crtp thing do - delegate to the derived class.

I worked around the issue by passing the dependent type as an additional template parameter, but I don’t like having to do this since it breaks my encapsulation:

template <typename T, typename U>
class Base {
public:
    using int_t = U;
};

class Derived : public Base<Derivedint> {
public:
    using int_t = int;
};

Why doesn’t my first example work?

Warmest regards, Matt

Richard Smith

unread,
Sep 12, 2016, 1:45:28 PM9/12/16
to std-dis...@isocpp.org
The CRTP base class is instantiated before the definition of the derived class is instantiated, so Derived::int_t does not yet exist. The CRTP pattern normally works around this by putting all uses of the derived class into member functions, whose instantiations are delayed until they themselves are used (typically after the definition of the derived class is complete).
 
Warmest regards, Matt

--

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

Karsten Ahnert

unread,
Sep 13, 2016, 1:13:20 AM9/13/16
to std-dis...@isocpp.org
I think I understand why it is currently not possible to use this kind of CRTP base class. But I wonder wether it is possible to implement such a feature in the C++ language? It seems that Derived needs to be parsed before Base<Derived> can be instantiated. I am not sure if this is possible in general. Any ideas?

Thanks, Karsten

Thiago Macieira

unread,
Sep 13, 2016, 12:12:05 PM9/13/16
to std-dis...@isocpp.org
On segunda-feira, 12 de setembro de 2016 22:13:18 PDT 'Karsten Ahnert' via ISO
C++ Standard - Discussion wrote:
> I think I understand why it is currently not possible to use this kind of
> CRTP base class. But I wonder wether it is possible to implement such a
> feature in the C++ language? It seems that Derived needs to be parsed
> before Base<Derived> can be instantiated. I am not sure if this is possible
> in general. Any ideas?

No, it's not. For Derived to be parsed, the compiler needs to parse its base
class, which is Base<Derived>.

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center

Reply all
Reply to author
Forward
0 new messages