index 可變模板展開

來源:互聯網
上載者:User

標籤:繼承   span   volatile   dex   decltype   a.out   vol   ...   可變   

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,                            nullptr, nullptr),#else        nullptr,#endif        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";    if (std::is_lvalue_reference<T>::value)        r += "&";    else if (std::is_rvalue_reference<T>::value)        r += "&&";    return r;}using namespace std;std::string demangle(const char* name);inline std::string demangle(const char* name){    int 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()    {        Init(demangle(typeid(this).name()));    }    void Init(std::string id)    {        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<<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.out Creating MakeIndexes<0, 0, 1, 2>*Creating MakeIndexes<1, 1, 2>*Creating MakeIndexes<2, 2>*Creating MakeIndexes<3>**************************************MakeIndexes<3>IndexSeq<0, 1, 2>

 

繼承關係:

MakeIndexes<3> :  MakeIndexes<2, 2>MakeIndexes<2, 2> : MakeIndexes<1,1,2>MakeIndexes<1,1,2>: MakeIndexes<0, 0,1,2>

 

index 可變模板展開

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.