Partial member function template specialisation and data member access

后端 未结 3 557
长情又很酷
长情又很酷 2021-01-13 16:22

I have a question regarding partial specialisation of templated member functions.

Background: The goal is to compute descriptive statistics of larg

3条回答
  •  臣服心动
    2021-01-13 16:54

    As commented, you don't need to use partial specialization for this at all, indeed partial specialization is usually pretty easy to avoid, and preferred to avoid.

    private:
    template 
    struct tag{}; // trivial nested struct
    
    template  
    void push_impl(I first, I last, tag) { ... } // generic implementation
    
    template 
    void push_impl(I first, I last, tag>) { ... } // complex implementation
    
    public:
    template 
    void push(InputIt first, InputIt last)
    {
        push_impl(first, last,
                  tag::value_type> {});
    }
    

    Since push_impl is a (private) member function you don't need to do anything special any more.

    Compared to your proposed solutions, this has no extra performance cost. It's the same number of function calls, the only difference is passing a stateless type by value, which is a wholly trivial optimization for the compiler. And there's no sacrifice in encapsulation either. And slightly less boilerplate.

提交回复
热议问题