CRTP - access to elements of incomplete type

Related Questions: One , Two

After trying to understand CRTP for several days, it seems that now I understand even less than before :)

Consider the following code:

01 #include <iostream>
02 
03 template <class IMPL>
04 class Interace
05 {
06 public:
07     typedef typename IMPL::TYPE TYPE;  // ERROR: "...invalid use of incomplete type..."
08     void foo() { IMPL::impl(); }       // then why does this work?
09 };
10 
11 class Implementation : public Interface<Implementation>
12 {
13 public:
14    typedef int TYPE;
15    static void impl() { std::cout << "impl() " << std::endl; }
16 };
17 
18 
19 int main()
20 {
21     Implementation obj;
22     obj.foo();
23 }

Questions:

  • Why can I call a function from IMPL::(line 8) but cannot access the type of types (line 7)? A related question says what IMPLis an incomplete type at this point. But why is line 8 correct?

  • What is this declaration / type definition order? As I can see:

    a. InterfaceThe template is OK. Creates no problems until instantiation

    b. line 11 - after class Implementation- the Implementationtype is declared, but not defined.

    . 11 - Interface<Implementation> - . Implementation ( !) - (b). "" IMPL Implementation. , , 7, 8 , , Implementation . , ?

, , Instantiation 21? 07 ?

, , ++ . .

+4
1

, , -, . - odr ( , ).

class Implementation : public Interface<Implementation>, Interface<Implementation>. Implementation , TYPE . , Interface<Implementation>::foo , main. Implementation .

+9

Source: https://habr.com/ru/post/1628946/


All Articles