- 浏览: 401090 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 15345首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1021RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 773It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 972As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1065typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 781There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 830we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 992As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 755There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 961Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 981As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 1000As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1335in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 944class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 991template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1214There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 861the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 594The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1173In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 877A 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 ...
### C++ Templates 全览:深入理解泛型编程的核心概念 C++的泛型编程,以其高度的灵活性和强大的功能,成为了继面向对象技术之后,C++领域内最受关注的技术焦点之一。《C++ Templates全览》这本书由侯捷、荣耀、...
在这个名为"data-structures-and-instantiation-patterns"的项目中,我们将会探讨这些核心概念。 【数据结构】 1. 数组:JavaScript中的基本数据结构之一,可以存储一系列有序的元素。通过索引进行访问,支持多种...
模板参数(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 ...
从给定的文件信息来看,虽然标题和描述指向的是“数据结构与算法分析”这本书,但实际内容却涉及另一本书《C++ Templates全览》,这本由侯捷等人翻译的书深入探讨了C++中的模板(Templates)概念,这是C++语言中用于...
- **模板实例化(Template Instantiation)**:编译器如何为特定模板参数生成具体代码的过程。 - **名字查找在模板(Looking Up Names in Templates)**:在模板上下文中如何查找符号名的规则。 #### 六、性能优化与...
Javascript 实例化模式JavaScript 提供了四种不同的实例化模式。 他们是: 功能性功能共享原型伪古典这个 repo 展示了如何实现所有这四种实例化模式。 每个模式的功能在堆栈和队列的使用中显示。...
该项目提供了一个所谓的 。 我不是唯一一个试图构建这样一个东西的人: : 示例: $instance = new self();... array( 0: Stmt_[removed] expr: Expr_Assign( var: Expr_Variable( name: instance ...
This feature significantly improves error messages related to template instantiation, which were notoriously difficult to understand in previous versions of C++. Concretely, concepts provide a ...
Template的“具现”行为(Template Instantiation) Template的错误报告(Error Reporting within a Template) Template中的名称决议方式(Name Resolution within a Template) Member Function的具现行为(Member...
分析JavaScript实例化模式四种模式JavaScript有4种不同的对象实例化模式。 JavaScript不是使用类,而是实现功能类(构造函数)以生成具有相似属性和方法的多个对象。 每个函数构造函数都执行四个基本操作: 创建一个...
Design 9Factory Method - MotivationFactory Method pattern is useful when you want to defer the instantiation to subclasses. This allows you to define the type of objects to create while letting ...
由于Typescript的堆栈限制,某些regEx因为它们太长而停止工作,并触发称为Type instantiation is excessively deep and possibly infinite递归堆栈溢出,其Type instantiation is excessively deep and possibly ...