T的模板专业化->std :: vector< T> [英] Template Specialization for T -> std::vector<T>
问题描述
我有一个模板类方法
template<class T>
T pop<T>();
现在,我想按以下方式进行模板专业化,
Now I want to do a template specialization as follows,
template<class T>
std::vector<T> pop<T>();
我可以做到以下几点,
template<>
std::vector<int> classname::pop<std::vector<int>>();
但是我仍然需要将类型保留为模板参数.我该怎么做?
But I still need to leave the type as a template parameter. How do I accomplish this?
推荐答案
此答案最初由体内的 Austin Salgat 提供问题 T的模板专业化->std :: vector (根据CC BY-SA 3.0许可证发布),并已作为答复移至此处,以遵守站点的Q& A格式.
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.
多亏了Piotr,我最终使用了标签分派.下面是代码我最终要做的事,
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<std::vector<int>>(12);
template<class T>
void push(T data) {
push(tag<T>(), data);
}
// The private method that calls the actual vector push for vector types
template<class T>
void push(tag<std::vector<T>>, std::vector<T> const& data_vector) {
push_vector(data_vector);
}
// The actual implementation
template<class T>
void push_vector(std::vector<T> const& data_vector) {
// Actual implementation in here
}
这篇关于T的模板专业化->std :: vector< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!