Decuing Types 类型推断
template<typename T>
void f(ParamType param);
f(expr);
在编译的时候,编辑器使用expr去推导两个类型:T 和 ParamType。例如:
template<typename T>
void f(const T& param);
int x = 0;
f(x);
这样T被推导为int,但是paramType被推导为const int&.
T的推导不仅依赖于expr,同时依赖于ParamType。有三种情况:
- ParamType是一个指针或者引用类型:
a. f expr’s type is a reference, ignore the reference part.
b. Then pattern-match expr’s type against ParamType to determine T. - ParamType是一个全局引用
• If expr is an lvalue, both T and ParamType are deduced to be lvalue references.
That’s doubly unusual. First, it’s the only situation in template type deduction
where T is deduced to be a reference. Second, although ParamType is declared
using the syntax for an rvalue reference, its deduced type is an lvalue reference.
• If expr is an rvalue, the “normal” (i.e., Case 1) rules apply.
- ParamType既不是指针也不是全局引用
posted on 2021-08-04 09:59 Ultraman_X 阅读(47) 评论(0) 收藏 举报