- 浏览: 399789 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 C++, there are function template, where you can have two type of template paramters, one is normal template parameter, which stands for a type, while the other is a template non-type parameter, which represents a potential value, such as the size of an array .
template instantiation is the process of an individual function constructed given a set of one or more actual type or values (for template non-type parameters).
the template instantiation is capable of deducing the value of those template non-type parameter, below shows the example and the reason analysis belows it.
NOTE: The process of determine the types and values of the templates argument from the type of type of the function argument is called "template argument deduction", we will have separate chapter t elaborte on this.
/** * @Summary * template non-type parameter * @Comment: * a non-type parameter consists of ordinary parameter declaration, A template non-type parameter indicate a constant in the template definition * an example of the non-type parameter is 'size' which represents the size of an array to which arr refers as follow. * * below shows something about the template function initialization when non-type parameter is instantiated * * the results show that the compiler should be able to infer the value of the non-type parameter if it can, and there is no need to provide a explicit * value for such parameter, in this case * since * int ia[] = { 10, 7, 14, 3, 25}; * has know size of * 5 * and when you call * min(ia); * it will match agains the definition * Type min(const Type (&r_arr)[size]) * so we know * size == 5 * so you don't need to write as * min(ia, 5); * and it will error, if you try to * min(ia, 5) */ template<typename Type, int size> Type min(const Type (&r_arr)[size]) { Type min_val = r_arr[0]; for (int i = 1; i < size; i++) { if (r_arr[i] < min_val) { min_val = r_arr[i]; } } return min_val; } // #include <iostream> using namespace std; // suppose we hve two array whose size is known at conpile time int ia[] = { 10, 7, 14, 3, 25 } ; double da[] = { 10.2, 7.1, 14.5, 3.2, 25.0, 16,8 } ; int _tmain(int argc, _TCHAR* argv[]) { int i = min(ia); if (i != 3) { cout << "?? oops: integer min() failed\n"; } else { cout << "!!OK: integer min() worked\n"; } // instantiation of min() for an array o f6 doubles // with type => double, size => 6 double d = min(da); if (d != 3.2) { cout << "??oops: double min() failed\n"; } else { cout << "!!ok: double min() worked\n"; } return 0; }
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15342首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1012RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 772It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 963As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1060typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 771There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 823we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 977As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 754There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 960Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 975As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 994As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1328in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 939class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 986template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1212There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 854the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 588The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1170In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 873A class member operator new() c ...
相关推荐
- **Class Template Argument Deduction:** Use CTAD (Class Template Argument Deduction) to make template class instantiation more concise. - **Designated Initializers:** Use designated initializers for ...
在这个名为"data-structures-and-instantiation-patterns"的项目中,我们将会探讨这些核心概念。 【数据结构】 1. 数组:JavaScript中的基本数据结构之一,可以存储一系列有序的元素。通过索引进行访问,支持多种...
### C++ Templates 全览:深入理解泛型编程的核心概念 C++的泛型编程,以其高度的灵活性和强大的功能,成为了继面向对象技术之后,C++领域内最受关注的技术焦点之一。《C++ Templates全览》这本书由侯捷、荣耀、...
模板参数(Template parameters)、模板特化的名称(Names of template specializations)、模板参数(Template arguments)、类型等价(Type equivalence)、模板声明(Template declarations)、名称解析(Name ...
- **非类型参数(Non-Type Parameter)**:非类型参数是除了类型外的其他参数,通常是常量表达式,如整型、指针等。 3. **模板实例化(Template Instantiation)** - 当我们使用模板定义的函数或类时,编译器会...
- **底层运作机制**: 如Template Argument Deduction、Template Overload Resolution、Looking Up Names in Templates、Template Instantiation等。 - **肌理分析**: 包括One Definition Rule (ODR)、Empty Base ...
- **模板实例化(Template Instantiation)**:将模板转换为具体的类型的过程。 **4. 肌理分析:** - **一次定义规则(One Definition Rule, ODR)**:确保一个程序中每个对象或函数只有一个定义。 - **空基类优化...
此外,模板衍生出的各种泛型技术和研究成果,如STL、Loki和Boost等,都有针对性的书籍帮助读者学习,例如《Generic Programming and the STL》、《Effective STL》、《Modern C++ Design》、《The C++ Standard ...
从给定的文件信息来看,虽然标题和描述指向的是“数据结构与算法分析”这本书,但实际内容却涉及另一本书《C++ Templates全览》,这本由侯捷等人翻译的书深入探讨了C++中的模板(Templates)概念,这是C++语言中用于...
- **模板实例化(Template Instantiation)**:编译器如何为特定模板参数生成具体代码的过程。 - **名字查找在模板(Looking Up Names in Templates)**:在模板上下文中如何查找符号名的规则。 #### 六、性能优化与...
Template的“具现”行为(Template Instantiation) Template的错误报告(Error Reporting within a Template) Template中的名称决议方式(Name Resolution within a Template) Member Function的具现行为(Member...
Javascript 实例化模式JavaScript 提供了四种不同的实例化模式。 他们是: 功能性功能共享原型伪古典这个 repo 展示了如何实现所有这四种实例化模式。 每个模式的功能在堆栈和队列的使用中显示。...
该项目提供了一个所谓的 。 我不是唯一一个试图构建这样一个东西的人: : 示例: $instance = new self();... array( 0: Stmt_[removed] expr: Expr_Assign( var: Expr_Variable( name: instance ...
Template的“具现”行为(Template Instantiation) Template的错误报告(Error Reporting within a Template) Template中的名称决议方式(Name Resolution within a Template) Member Function的具现行为(Member...