how does the recursive template deduction works

137 Views Asked by At

I read some complex template class design, it looks like recirsive template deduction with template parameter refers to the template itself. I simplified the codes as follow, does anybody can help explain how the template deduction works for the follow codes. also I want to know more knowledge about the programming technique used in this demo and can you share me any c++ standard material about this topic.

#include <iostream>

template<typename X = char, class T = int>
class ABC;

template<class X, class T>
class ABC <X, ABC<X, T>>{
public:
    T t;
};


template<class T, class M>
class ABC : public ABC<T, ABC<T,M>> {};

int main() {
    ABC<int, char> abc;
    abc.t = 1;
    ABC<> abcd;
    abcd.t = 3;
    std::cout << abc.t << " " << abcd.t << std::endl;
}

as shown above, I tried simplified the code and write a little demo. my compiler is gcc 9.4.0 released with ubuntu20, with compile args -std=c++17.

What I concerned most is how abc or abcd is deduced as ABC<T, M> is inherited from ABC<T, ABC<T, M>>, and ABC<T, M> seems is a self-referencing template class?

And also how the template deduction terminated as it seems there is only a template class declaration template<typename X = char, class T = int> class ABC; and seems no implemention of it.

abc.t is print as unvisable char succeeding with a whitespace and then a charater 3, as below.

 3
0

There are 0 best solutions below