Template Specialization for T -> std::vector

后端 未结 4 818
情歌与酒
情歌与酒 2021-01-12 15:54

I have a template class method

template
T pop();

Now I want to do a template specialization as follows,



        
4条回答
  •  囚心锁ツ
    2021-01-12 16:12

    This answer was originally provided by Austin Salgat in the body of the question Template Specialization for T -> std::vector, (posted under the CC BY-SA 3.0 license), and has been moved here as an answer in order to adhere to the site's Q&A format.


    Thanks to Piotr I ended up using tag dispatching. Below is the code for what I ended up doing,

    // The public method that is accessed by class.push>(12);
    template
    void push(T data) {
        push(tag(), data);
    }
    
    // The private method that calls the actual vector push for vector types
    template
    void push(tag>, std::vector const& data_vector) {
        push_vector(data_vector);
    }
    
    // The actual implementation
    template
    void push_vector(std::vector const& data_vector) {
    // Actual implementation in here
    }
    

提交回复
热议问题