Https://www.zhihu.com/question/51253466#include <iostream>
#include <fstream> #include <memory> #include <iterator> #include <type_traits> #include < cstdlib> #include <memory> #include <cxxabi.h> #include <type_traits> #include <typeinfo># Ifndef _msc_ver# include <cxxabi.h> #endif # include <memory> #include <string> #include < Cstdlib>template <class t>std::string type_name () { typedef typename STD::REMOVE_REFERENCE<T >::type tr; Std::unique_ptr<char, Void (*) (void*) > own (#ifndef _msc_ver Abi::__cxa_demangle (typeID (TR). Name (), nullptr, &NBS P nullptr, nullptr), #else nullptr, #endif & nbsp std::free ); std::string r = own! = nullptr? Own.get (): typeID (TR). Name (); if (std::is_const<tr>::value)   R + = "Const"; if (std::is_volatile<tr>::value) r + = "volatile"; &nbs P if (std::is_lvalue_reference<t>::value) r + = "&"; else if (std::is_rval Ue_reference<t>::value) r + = "&&"; return R; Using namespace std;std::string demangle (const char* name); inline std::string demangle (const char* name) { in T status = -4; Std::unique_ptr<char, Void (*) (void*) > res { ABI: : __cxa_demangle (name, NULL, NULL, &status), std::free }; return ( status==0)? Res.get (): name;}
Template<int ... >struct indexseq {};template<int N, int ... Indexes>struct makeindexes:makeindexes<n-1, N-1, indexes...>{ makeindexes () {&nbs P Init (Demangle (typeID (this). Name ()); } void Init (std::string ID) & nbsp { std::cout<< "Creating" <<id<<std::endl; }}; Template<int ... Indexes>struct makeindexes<0, indexes...>{ typedef indexseq<indexes...> type; Makeindexes () { Init (Demangle (typeID (this). Name ())); } void Init (std::string id) { std::cout<< "Creating" <<id& lt;<std::endl; }};int Main () { makeindexes<3> a; Makeindexes<3>::type b; std::cout<< "*************************************" <<std::endl; Std::cout<<type_name<decltype (a) > () <<std::endl; Std::cout <<type_name<decltype (b) > () <<std::endl;}
[Email protected]_99_227_centos dev]#./a.outCreating makeindexes<0, 0, 1, 2>*creating makeindexes<1, 1, 2>*creating makeindexes<2, 2>*Creating Makeindexes<3>**************************************makeindexes<3>indexseq<0, 1, 2>
Inheritance Relationship:
Makeindexes<3>: Makeindexes<2, 2>makeindexes<2, 2>: makeindexes<1,1,2>makeindexes<1,1,2 : Makeindexes<0, 0,1,2>
Index variable Template expansion