C++ variadic template template argument that matches any kind of parameters

后端 未结 3 1189
野的像风
野的像风 2021-01-30 16:38

I was wondering if it\'s possible to write a template function that can take any other arbitrary template as a parameter and properly match the template name (i.e. not just the

3条回答
  •  抹茶落季
    2021-01-30 17:16

    You must have a metafunction that rebinds the type of container. Because you cannot just replace first template parameter:

    vector > input;
    vector > just_replaced;
    vector > properly_rebound;
    

    So, just write such a metafunction for known set of containers.

    template class rebinder;
    
    // example for vectors with standard allocator
    template class rebinder< std::vector, T > {
    public:
      typedef std::vector type;
    };
    // example for lists with arbitrary allocator
    template class rebinder< std::list, T > {
      typedef typename A::template rebind::other AT; // rebind the allocator
    public:
      typedef std::list type; // rebind the list
    };
    // example for arrays
    template class rebinder< std::array, T > {
    public:
      typedef std::array type;
    };
    

    Rules of rebinding may vary for different containers.

    Also you might require a metafunction that extracts value type from arbitrary container, not only std-conformant (typedef *unspecified* value_type)

    template class get_value_type {
    public:
      typedef typename Container::value_type type; // common implementation
    };
    template class get_value_type< YourTrickyContainer > {
      ......
    public:
      typedef YZ type;
    };
    

提交回复
热议问题