What are some uses of template template parameters?

后端 未结 10 1009
无人共我
无人共我 2020-11-22 03:41

I\'ve seen some examples of C++ using template template parameters (that is templates which take templates as parameters) to do policy-based class design. What other uses do

10条回答
  •  旧巷少年郎
    2020-11-22 03:57

    Say you're using CRTP to provide an "interface" for a set of child templates; and both the parent and the child are parametric in other template argument(s):

    template  class interface {
        void do_something(VALUE v) {
            static_cast(this)->do_something(v);
        }
    };
    
    template  class derived : public interface {
        void do_something(VALUE v) { ... }
    };
    
    typedef interface, int> derived_t;
    

    Note the duplication of 'int', which is actually the same type parameter specified to both templates. You can use a template template for DERIVED to avoid this duplication:

    template