- 浏览: 399078 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 discuss the template function specialiation ;
so what is "template function specialiation"?
given an example, for the following function template
template <class T> T max (T t1, T t2) { return (t1 > t2 ? t1 : t2); }it works for most value types, however what if the type passed in is a pointer, list const char *, or what if the function does not provide the less than operator and it provides some alternatives;
In such situation we might provide a specified template with a more explicity value of parameter type and define/declare the template's body for that specialized type parameter.
to declare the specialization of max for template argument of const char * (typedefed as PCC), you can do this :
// the below is the template specialization on the type argument of const char * (typedefed as PCC) typedef const char * PCC; using std::strcmp; template<> PCC max<PCC>(PCC t1, PCC t2) { return (strcmp(t1, t2) > 0 ? t1 : t2); }
there is on tip, if the template argument can be deduced, we can omit the <T> part after the function's name in the function's explicit specializationm, use this:
// it is possible to omit the template argument if the type of // template argument can be deduced from the the function parameters template<> PCC max(PCC t1, PCC t2) { return (strcmp(t1, t2) >0 ? t1 : t2); }
there are some pith/gist/quintessence/essence points that you may want to know about the function template expclit specialization.
1. specialiation visiblity rule
if you provide a function template specialiation, it should be visible in every file that uses it. otherwise, there would be compilation error (you can not hvae some instantiation for the same template argument from the generic template function, and some instantiation from the specializatoin)
normally, you should put the specialization in the header file with the generic template.
2. namespace rule
Second, the function template specialiation should be in the same namespace as the template that it specialize
below shows an error that can be generated through the code below.
// primer.h namespace cplusplus_primer { template <class type> type min(Type* array, int size) { /* ... * / } } // user.h class SmallInt { /* * / } // user.C template< > SmallInt min(SmallInt * array, int size); // this is an error, because it is a differente namespace from the generic template.
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15338首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1008RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 769It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 955As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1057typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 769There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 818we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 975As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 751There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 959Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 974As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 993As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1327in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 936class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 985template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1210There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 850the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 587The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1165In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 869A class member operator new() c ...
相关推荐
Templates: The Hard Way Templates: The C++ Way Function Specialization Class Templates Class Specialization Implementation Details Advanced Features Summary Programming Exercises 25. Standard ...
6. **模板的部分特化(Template Partial Specialization)** - 类模板的特性,允许为模板的部分参数提供特化。 - 例如,为只改变第一个类型参数的`MyClass, T2>`提供特化版本。 7. **模板元编程(Template ...
模板参数(Template parameters)、模板特化的名称(Names of template specializations)、模板...specialization)、函数模板特化(Function template specializations)详细阐述了C++模板的定义、特化和实例化过程...
Part 3: Generic Programming – Template Specialization Part 3: Generic Programming – Partial Specialization Part 3: Generic Programming – Names and Namespaces Part 3: Generic Programming – ...
此外,他还讲解了标准模板库(Standard Template Library, STL),包括容器(container)、迭代器(iterator)、算法(algorithm)和函数对象(function object),这些是C++高效编程的基础。 4. 第39讲:异常处理 ...
4. C++99标准:C++99是C++的一个重要版本,引入了若干新特性,如命名空间(namespace)、内联变量(inline variables)、局部类(local classes)、模板的部分特化(partial template specialization)、迭代器分类...
在源码中,你可以看到函数模板、类模板的使用,以及模板特化(template specialization)和模板元编程(metaprogramming)的示例。 最后,C++11和C++14引入了许多新特性,如右值引用(rvalue reference)、lambda...
- **模板特化**(Template Specialization):允许对特定类型或类型组合提供不同的模板实现。 - **模板元编程**(Template Metaprogramming):使用模板来在编译时执行计算和代码生成。 #### 6. 内联函数与内联变量...
4. **模板**:学习如何编写模板函数和模板类,以及模板特化(Template Specialization)和模板元编程(Template Metaprogramming)。 5. **STL(标准模板库)**:STL包括容器(如vector、list、set)、迭代器、算法...
- **模板特化(Template Specialization)**:允许为特定类型提供不同的实现,从而优化性能或实现特定于类型的行为。 STL概述: STL由以下几个主要部分组成: 1. **容器(Containers)**:提供了一系列数据结构...
包括函数模板和类模板,以及模板特化(template specialization)和模板元编程(metaprogramming)。 5. **STL(Standard Template Library)**:C++标准模板库包含容器(如vector、list、map等)、算法(如sort、...
##### 3.6 模板特化 (Template Specialization) Concepts 使得编写特定于类型的行为变得更加容易,通过特化可以为不同类型的输入提供不同的实现。 ```cpp template concept bool IsString = std::is_same, std::...
在C++中,多态性主要通过虚函数(Virtual Function)和纯虚函数(Pure Virtual Function)实现。学员将学习如何使用虚函数实现动态绑定,以及如何设计抽象类。 4. **模板(Template)**:C++模板提供了泛型编程的...
多范型设计还涉及到模板特化(template specialization)。在某些情况下,我们可能希望为特定的数据类型提供定制的实现,这时可以使用模板特化。例如,为模板函数或类模板定义一个专门处理某种类型的行为。 此外,...
其中,模板机制(template mechanism)是学习泛型技术及STL的基石,涉及函数模板(function templates)、成员模板(member templates)、特化(specialization)等概念。 为了更好地运用STL,学习者需要深入理解...
试题可能涵盖函数模板、类模板以及模板特化(template specialization)的概念。 9. **STL(Standard Template Library)** STL是C++的标准模板库,包括容器(如vector、list、set)、迭代器、算法和函数对象。...
1. **函数模板** (Function Template) 函数模板是为了解决相同功能但针对不同数据类型的函数重载。例如,`swap` 函数就是一个常见的例子,它用于交换两个变量的值。使用函数模板可以避免为每种数据类型编写单独的`...
1. **函数模板** (Function Template) 函数模板用于创建一组可重载的函数,它们共享相同的函数体,但能够处理不同类型的数据。以`swap`函数为例,传统的重载可能针对不同类型的变量编写多个版本,但函数模板只需要...
函数模板、类模板以及模板特化(template specialization)都是这一章的关键点。 第15章通常会讨论STL(Standard Template Library,标准模板库),这是C++库的核心部分。你会学到容器(container)如vector、list...