[C++] Template

本文深入探讨了C++中的函数模板和类模板,解释了如何使用泛型编程来创建类型灵活的代码。通过实例展示了模板参数的推导过程,并强调了模板定义在头文件中的必要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Both object oritend programming(OOP) and generic programming deal with types that are not known at the time the program is written. The distinction between the two is that OOP deals with types that are not known until run time, whereas in generic programming the types become known during compilation.

When we use a generic type, such as vector, or a generic function, such as find, we supply the information needed to transform that blueprint into a specific class or function. That transformation happens during compilation. 

 

Function Templates

A function template is a formula from which we can generate type-specific version of that function

template <typename T>
int compare(const T &v1, const T &v2)
{
    if(v1 < v2) return -1;
    if(v2 < v1) return 1;
    return 0;
}

A template definition starts with the keyword template followed by a template parameter list, which is a comma-separated list of one or more template parameters.

The compiler use the deduced template parameter to instantiate a specific verison of the function for us. When the compiler instantiate a template, it create a new "instance" of the template using the actual template arguments in place of the corresponding template parameters.

cout << compare(1, 0) << endl;            // T is int
vector<int> vec1{1, 2, 3}, vec2{4, 5, 6};
cout << compare(vec1, vec2) << endl;    // T is vector<int>

In above example, the compiler will instantiate two different version of compare. One for int, another for vector<int>.

 

Each type parameter must be preceded by the keyword class or typename.

// error: must precede U with either typename or class
template <typename T, U> 
calc(const T&, const &U);

// ok: no distinction between typename and class in a template parameter
template <typename T, class U>
cal(const T&, const &U);

 

Template compliation

When the complier see the definition of template, it does not generate code. It generate code only when we instantiate a specific instance of the template.

 

  • When we call a function, the compiler need to see only a declaration for the function.
  • When we use objects of class type, the compiler need to see the class definition(class declaration), while the definition of the member functions need not to present. 

So, we put class definition and function declaration in header file, and definiton of ordinary and class-member functions in the source files.

 

Template are different: To generate an instance, the compiler need to know the code that define a function template or class template member function.

So, we put both definition and declaration of templates in the header file.

 

Reference:

C++ Primer, Fifth Edition, chapter 16 Templates and Generic Programming

 

转载于:https://www.cnblogs.com/TonyYPZhang/p/6870273.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值