模板类型推导

类型自动推导是C++模板编程中的一个重大特性,很多时候我们不会显式特化模板参数类型,而是由编译器根据表达式类型自动推导出正确的模板参数类型。而理解模板类型推导原则可以帮助我们更有效地理解和使用C++模板编程。

首先我们要清楚认识到模板类型推导的重要性。

  1. 函数重载与SFINAE。(包含函数模板的重载匹配)
  2. 模板特化。最常见的在STL中针对指针类型的特化。
  3. 右值引用的支持。perfect forwarding

我们常见的模板类型推导的例子如下:

template<typename T> void f(ParamType param);   // declaration or definition
f(expr);   // caller

三个重要术语: TParamTypeexpr
在编译期,编译器会根据 expr 的类型 自动推导 TParamType 的类型。

非数组和函数指针类型的 expr,其推导规则有3类。其中有一类涉及到universal reference 这种C++11引入的新型引用,我们首先来了解一下什么是 universal reference

universal reference

universal reference 有时也被称为 forwarding reference,根据 expr 的不同,可能导致 T 是一个左值引用,或者一个右值引用。

同时满足以下两个条件的引用 才是 universal reference

  • 必须存在类型推导
  • ParamType 只能为 T&&const T&& 都不行。

一般情况下只出现在 模板函数 和 auto 这两种情况下。

template<typename T> void f(T&& param)    // univeral reference
auto&& t2 = t1;                           // universal reference

void f(T&& param)      // rvalue reference.   (no type deduction)
template<typename T> 
void f(vector<T>&& param)   // rvalue reference  (no T&&)
template<typename T> 
void f(const T&& param)     // rvalue reference  (no T&&)

非数组类型推导

根据ParamType,类型推导 可以分为 3 大类:

1、ParamType 是一个 引用(非 universal reference)或者指针。

template<typename T> void f(T& param)              // lvalue reference
template<typename T> void cf(const T& param)  // const-lvalue reference
template<typename T> void rf(vector<T>&& param)    // rvalue reference
template<typename T> void pf(T* param)             // pointer

推导原则(指针和引用相同):

1.1. if expr’s type is reference, ignore the reference part.
1.2. then, pattern-match expr’s type against ParamType to determine T. ignore the ref part.

例子:

int x = 27;
const int cx = x;
const int& xr = x;

f(x)        // expr(x) is int,  T is int
f(cx)       // expr(cx) is const int,  T is const int
f(xr)       // expr(xr) is const int&, ignore reference,  T is const int

cf(x)               // ParamType is const int &, ignore &, T is int
cf(cx)              // T is int
cf(xr)              // T is int

2、ParamType 是一个 universal reference

template<typename T> void f(T&& param)

推导规则:

  • if expr is lvalue, both T and ParamType are lvalue reference.
  • if expr is rvalue, same with case1.

例子:

int x = 27;                           
const int cx = x;
const int& xr = x;

f(x)                 // x is lvalue,  T and ParamType are int&
f(cx)                // cx is lvalue, T and ParamType are const int&
f(xr)                // xr is lvalue, T and ParamType are const int&
f(27)                // 27 is rvalue, T is int, so ParamType is int&&

3、ParamType既不是引用也不是指针

template<typename T> void f(T param)

这种pass-by-value的情况,会创建一个新的对象,从而和expr的关系不是很紧密了。

推导规则:
3.1. if expr’s type is reference, ignore reference
3.2. then, remove expr’s const and volatile.

f(x)              // T and param's type are both int
f(cs)             // T and param's type are both int
f(xr)             // T and param's type are both int
// some tricky
const char* const prt = "Fun with pointer";
f(ptr)           // expr is a const-pointer and pointer to a const object,
                 // just remove the constness of ptr, not the object pointer to.
                 // T and param's type are const char*

数组类型推导

1. pass-by-value

数组类型会 decay 为指针类型。

template<typename T> void f(T param);
const char name[] = "Steve Jobs";

f(name)     // expr is const char[11],  but T deducted to const char*

2、 pass-by-reference

数组类型就是数组类型,不会 decay

template<typename T> void f(T& param);
const char name[] = "Steve Jobs";

f(name)     // expr is const char[11],  and T deducted to const char[11].
            // So ParamType is const char(&)[11]

函数类型推导

这种情况和 数组类型推导一样。 pass-by-value时,会 decay 为函数指针。

void someFunc(int, double);

template<typename T> void f1(T param);
template<typename T> void f2(T& param);

f1(someFunc)               // T and ParamType deducted to void (*)(int, double)
f2(someFunc)               // T deducted to void ()(int, double), ParamType is void (&)(int, double)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
模板参数推导(template argument deduction)是C++11中引入的一个新特性,它可以自动推导模板参数,避免手动指定模板参数。 在使用模板函数或模板类时,通常需要手动指定模板参数,例如: ``` template<typename T> void foo(T t); int main() { foo<int>(1); // 指定模板参数为int return 0; } ``` 在上面的代码中,我们调用了模板函数`foo`,并手动指定了模板参数为int。这种方式比较繁琐,容易出错。 模板参数推导可以自动推导模板参数,避免手动指定模板参数。例如: ``` template<typename T> void foo(T t); int main() { foo(1); // 自动推导模板参数为int return 0; } ``` 在上面的代码中,我们调用了模板函数`foo`,并没有手动指定模板参数,编译器会自动推导模板参数为int。 模板参数推导可以用于函数模板和类模板的实例化过程中。它可以根据函数参数或对象成员的类型自动推导模板参数的类型。例如: ``` template<typename T, int N> class Array { public: T data[N]; }; int main() { Array arr{1, 2, 3}; // 自动推导模板参数为int和3 return 0; } ``` 在上面的代码中,我们定义了一个类模板`Array`,它有两个模板参数,一个是类型参数T,一个是整参数N。在实例化`Array`对象时,我们只传入了一个参数{1, 2, 3},编译器会自动推导模板参数为int和3。 需要注意的是,模板参数推导只能用于函数模板和类模板的实例化过程中,不能用于模板定义中。在模板定义中,必须手动指定模板参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值