`

c++ - template non-type parameter and template instantiation

    博客分类:
  • c++
c++ 
阅读更多

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;
}
分享到:
评论

相关推荐

    Google C++ Style Guide_英文版.pdf

    - **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

    在这个名为"data-structures-and-instantiation-patterns"的项目中,我们将会探讨这些核心概念。 【数据结构】 1. 数组:JavaScript中的基本数据结构之一,可以存储一系列有序的元素。通过索引进行访问,支持多种...

    C++ 标准 ISO 14882-2011

    模板参数(Template parameters)、模板特化的名称(Names of template specializations)、模板参数(Template arguments)、类型等价(Type equivalence)、模板声明(Template declarations)、名称解析(Name ...

    C++ Templates

    - **非类型参数(Non-Type Parameter)**:非类型参数是除了类型外的其他参数,通常是常量表达式,如整型、指针等。 3. **模板实例化(Template Instantiation)** - 当我们使用模板定义的函数或类时,编译器会...

    《C++ Templates 全览》

    - **底层运作机制**: 如Template Argument Deduction、Template Overload Resolution、Looking Up Names in Templates、Template Instantiation等。 - **肌理分析**: 包括One Definition Rule (ODR)、Empty Base ...

    c++ templates 全览(侯捷)

    - **模板实例化(Template Instantiation)**:将模板转换为具体的类型的过程。 **4. 肌理分析:** - **一次定义规则(One Definition Rule, ODR)**:确保一个程序中每个对象或函数只有一个定义。 - **空基类优化...

    C++.Templates.-.The Complete.Guide(侯捷译)

    此外,模板衍生出的各种泛型技术和研究成果,如STL、Loki和Boost等,都有针对性的书籍帮助读者学习,例如《Generic Programming and the STL》、《Effective STL》、《Modern C++ Design》、《The C++ Standard ...

    数据结构与算法分析--C++描述(第3版)

    从给定的文件信息来看,虽然标题和描述指向的是“数据结构与算法分析”这本书,但实际内容却涉及另一本书《C++ Templates全览》,这本由侯捷等人翻译的书深入探讨了C++中的模板(Templates)概念,这是C++语言中用于...

    C++ Templates(侯捷版1-10章).pdf

    - **模板实例化(Template Instantiation)**:编译器如何为特定模板参数生成具体代码的过程。 - **名字查找在模板(Looking Up Names in Templates)**:在模板上下文中如何查找符号名的规则。 #### 六、性能优化与...

    《深度探索C++对象模型》(Stanley B·Lippman[美] 著,侯捷 译)

    Template的“具现”行为(Template Instantiation) Template的错误报告(Error Reporting within a Template) Template中的名称决议方式(Name Resolution within a Template) Member Function的具现行为(Member...

    Javascript-Instantiation-Patterns:功能、功能共享、原型和伪经典实例化方法的演示

    Javascript 实例化模式JavaScript 提供了四种不同的实例化模式。 他们是: 功能性功能共享原型伪古典这个 repo 展示了如何实现所有这四种实例化模式。 每个模式的功能在堆栈和队列的使用中显示。...

    php-parser-instantiation-printer:用于打印实例化给定 PHP-Parser 节点所需的代码

    该项目提供了一个所谓的 。 我不是唯一一个试图构建这样一个东西的人: : 示例: $instance = new self();... array( 0: Stmt_[removed] expr: Expr_Assign( var: Expr_Variable( name: instance ...

    深度探索C++对象模型 超清版

    Template的“具现”行为(Template Instantiation) Template的错误报告(Error Reporting within a Template) Template中的名称决议方式(Name Resolution within a Template) Member Function的具现行为(Member...

    C++Templates(中文版)

    ### C++ Templates 全览:深入理解泛型编程的核心概念 C++的泛型编程,以其高度的灵活性和强大的功能,成为了继面向对象技术之后,C++领域内最受关注的技术焦点之一。《C++ Templates全览》这本书由侯捷、荣耀、...

Global site tag (gtag.js) - Google Analytics