- 浏览: 389210 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
in this post, we are going to examining partialization.
There are two kind of partialization, one is full specialization, and the other is partial specialization.
- full specialization
- partial specialization
Full specialization
class
- QueueItem
- Queue
Are used for full specialization, by full specialization, it means once specialized, there exist only one instance for the specialized template arguments.
see the code below.
/** * file * template_specialization.h * description: * template specialization and partial specialization */ #include <iostream> #include <string> #include <assert.h> #include <cassert> using std::string; using std::ostream; using std::istream; using std::cerr; using std::cout; using std::endl; using std::exit; /** * * class * QueueItem * Queue * are used for full specialization, by full specialization, it means once specialized, there exist only one instance for the specialized template arguments. * * Later we will discuss parital specialization */ template<class Type> class QueueItem { public: QueueItem(const Type &val) : next(0), value (val) {} QueueItem<Type> *next; Type value; protected: }; template <class Type> class Queue { public: Queue() : front(0), back (0) {} ~Queue() ; Type remove(); Type min(); Type max(); void add(const Type &); bool is_empty() const { return front == 0; } // class member template // with this you can define // things like Queue<int>::CL<string> ... // which may sound useless at the frist glance, but we will see template <class T> class CL { T name; Type mem; }; template <class Iter> void assign(Iter first, Iter last) { while (!is_empty()) remove(); // call Queue<T>::remove for (; first != last; ++first) add(*first); } private: QueueItem<Type> *front; QueueItem<Type> *back; protected: }; class LongDouble { public: LongDouble(double dval) : value(dval) {} bool compareLess(const LongDouble & rhs) { return value < rhs.value; } private: double value; protected: }; template <class Type> Queue<Type>::~Queue() { while (!is_empty()) { remove(); } } template <class Type> void Queue<Type>::add(const Type &val) { QueueItem<Type> *pt = new QueueItem<Type>(val); if (is_empty()) { front = back = pt; } else { back->next = pt; } } template <class Type> Type Queue<Type>::remove() { if (is_empty()) { cerr << "remove() on empty queue\n"; exit(-1); } QueueItem<Type> *pt = front; front = front->next; Type retval = pt->value; delete pt; return retval; } // find minimum value in the queue template <class Type> Type Queue<Type>::min() { assert(! is_empty()); Type min_value = front->value; for (QueueItem<Type> * pq = front->next; pq != 0; pq = pq->next) { if (pq->value < min_val) min_val = pq->value; } return min_val; } template <class Type> Type Queue<Type>::max() { assert(! is_empty()); Type max_val = front->value; for (QueueItem<Type> * pq = front->next; pq != 0; pq = pq->next) { if (pq->value > max_val); max_val = pq->value; } return max_val; } /** * template<> * LongDouble Queue<LongDouble>::min() * * this is the specialization of member * which once is defind, will create a specialization on the class of template<> class Queue<LongDouble>; * * also, if the specialization will totally redefine the data members, not just the member functions, you can try the * full specialization on class as well. * * template<> * class <void *> Queue{ }; * * you cannot do * specialization member * specialization on the class * at the same time * * this is full specialization, while you can do partial specialization, please refer the partial specialization later on this chapter */ template <> LongDouble Queue<LongDouble>::min() { assert(! is_empty()); LongDouble min_val = front->value; for (QueueItem<LongDouble> * pq = front->next; pq != 0; pq = pq->next) { if (pq->value.compareLess(min_val)) min_val = pq->value; } return min_val; } /** * template<> * LongDouble Queue<LongDouble>::min() * */ template <> LongDouble Queue<LongDouble>::max() { assert(! is_empty()); LongDouble max_val = front->value; for (QueueItem<LongDouble> * pq = front->next; pq != 0; pq = pq->next) { if (!pq->value.compareLess(max_val)) max_val = pq->value; } return max_val; }
As we have described in the code above, you can do template specialization on the data member, you can also do specialization on the the template class itself. as below.
template<> class Queue<void *> { public: Queue() : front(0), back (0) {} ~Queue() ; void* remove(); void* min(); void* max(); private: QueueItem<void *> *front; QueueItem<void *> *back; protected: };
while, let me restate/reiterate/rephraze/recapitulate the that
you cannot do
- specialization on member function
- specialization on class template
at the same time.
partial specialization
unlike the full specialization, which will have only one template instance for the particular specialized type parameter. while, partial specialization results in another template, which has infinite set of instances.
let's see an example.
/** * * class * Screen * are used for partial specialization, by partial which means after the specialization, there still exist inifinite set of template instances based on the unspecialized type parameter. */ template<int hi, int wid> class Screen { //... }; /** * fixed (specialized) the wid non-type template parameter to 80, if the template class has type template parameter, we can fixed the */ template<int hi> class Screen<hi, 80> { public: Screen(); private: string _screen; string::size_type _cursor; short _height; // use the special }; /** * template<int hi> * Screen<hi, 80>::Screen() * */ template<int hi> Screen<hi, 80>::Screen() : _screen(""), _cursor(string("").size), _height(80) { }
while the above code shows partial specialization on the type parameters. let's seen the example.
/* * template<class T1, class T2> * class Foo {} * * shows how to do partial specialization on type parameter template class. */ template<class T1, class T2> class Foo { }; template<class T1> class Foo<T1, string> { };
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15284首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 965RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 744It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 919As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1031typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 735There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 783we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 913As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 721There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 931Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 924As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 950As we all know that we virtual ... -
c++ - member template in class template
2012-09-26 08:19 903class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 957template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1181There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 802the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 553The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1133In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 837A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1126You can overload the subscript ...
相关推荐
specialization-coursera specialization-coursera
Machine-Learning-Specialization---UW
网易云课堂,吴恩达老师DL微课程的课后测试题和编程习题,
Partial template specialization Typelists—powerful type manipulation structures Patterns such as Visitor, Singleton, Command, and Factories Multi-method engines For each generic component, the ...
吴恩达的机器学习课程主要包括两门,一门是在Cousera上的《机器学习》,另一门是他在斯坦福大学教授的《CS229: Machine Learning》。 Cousera上的《机器学习》课程侧重于概念理解,而不是数学推导。...
自然语言处理专业化本仓库为存放相关课程课后作业代码,详细见博客博客地址:https://blog.csdn.net/weixin_43093481/article/details/114989382使用方法:下载整个文件夹到本地,使用jupyter notebook打开即可
Deep-Learning-Specialization
Deep-Learning-Specialization-Sequence-Model
数据结构与算法专业化
数据结构和算法专业化该存储库包含Coursera的“数据结构和算法专业化”课程中的代码,以及Geeks for Geeks中针对DSA问题的其他解决方案(此存储库是在我编写DSA的过程中创建的,因此很少有代码是完全相同的解决方案...
- [Dynamic Programming: Model-Based RL, Policy Iteration and Value Iteration](DP/)动态规划 - [Monte Carlo Model-Free Prediction & Control](MC/) MC蒙特卡洛算法 - [Temporal Difference Model-Free ...
#JohnsHopkinsUni的《 Coursera-Data-Science-Specialization》 数据科学家的工具 R编程 获取和清理数据 探索性数据分析 可重复的研究 数据科学:统计学和机器学习专业 统计推断 回归模型 实用机器学习 开发数据...
coursera-algorithms-specialization:数据结构和算法专业化
数据科学课程来自Coursera.org的Johns Hopkins的数据科学课程资料库
MATLAB为工程师和科学家编程的专业化该存储库代表Coursera专业化工程师和科学家的MATLAB编程专业化
Tensorflow-Advanced-Specialization:此Coursera专业领域的作业和Jupyter笔记本
机器学习专业数据集课程一亚马逊宝贝 home_data image_test_data ... Inferring networks of substitutable and complementary productsJ. McAuley, R. Pandey, J. LeskovecKnowledge Discovery and Data Mining, 2015
coursera-python-specialization 来自密歇根大学的Coursera Python专业化课程 面向所有人的编程 :check_mark: Python数据结构 :check_mark: 使用Python访问Web数据 :check_mark: 在Python中使用数据库 :check_mark:...