Extract C++ template parameters

前端 未结 5 1781
轻奢々
轻奢々 2020-12-05 13:26

Although I\'m doubtful, I\'m curious as to whether it\'s possible to extract primitive-type template parameters from an existing type, perhaps using RTTI.

For exampl

5条回答
  •  Happy的楠姐
    2020-12-05 13:44

    In the case of std::bitset, you can just use the size() member function:

    size_t sz = oh_my_word.size();  // sz is now 16
    

    In the general case, you can define a member function which returns the size in a similar manner:

    template 
    class Foo
    {
    public:
      int size() const { return N; }
    };
    

提交回复
热议问题