模板推导趣事,c++
Template deduction interesting case, c++
考虑一下代码的和平:
template<class T>
void f(const T& t)
{
static int x = 0;
cout<<++x<<endl;
}
int main()
{
int j = 0;
const int i = 0;
f(5);
f(i);
f(j);
}
我已经调用了 3 种类型的函数。虽然 5 和 j 可以是同一个东西,只是 int,const int i 肯定是不同的类型。
但无论如何我的输出是:
1
2
3
这意味着编译器为不同的类型实例化相同的函数。
我对么?谁能解释一下为什么?
此处 f
将针对类型 int
实例化一次,因为所有 3 次调用都只调用 f<int>
.
只要类型推导有效就正确了。
考虑一下:
#include <iostream>
template<class T>
void f(const T& t)
{
static int x = 0;
std::cout<< "x=" << ++x<<std::endl;
}
template<class T>
void f(T&& t)
{
static int y = 0;
std::cout<< "y=" << ++y<<std::endl;
}
template<class T>
void f(T& t)
{
static int z = 0;
std::cout<< "z=" << ++z<<std::endl;
}
int main()
{
int j = 0;
const int i = 0;
f(5);
f(i);
f(j);
}
输出:
y=1
x=1
z=1
来自[temp.deduct.call]:
Template argument deduction is done by comparing each function template parameter type (call it P) with
the type of the corresponding argument of the call (call it A) as described below.
P
是const T&
,A
是int
,int
,const int
三个调用。
然后我们有:
If P is a reference type, the type referred to by P is used for type deduction.
P
是引用类型,所以我们用P' == const T
来推导A == int
或A == const int
。在这两种情况下,我们都推导 T == int
以便 P' == const int
(和 P == const int&
)和推导的 A == const int
。这比前两次调用的原始 A
更 cv 合格,但明确表示可以:
In general, the deduction process attempts to find template argument values that will make the deduced A
identical to A (after the type A is transformed as described above). However, there are three cases that allow
a difference:
— If the original P is a reference type, the deduced A (i.e., the type referred to by the reference) can be
more cv-qualified than the transformed A.
因此,所有三种情况都只调用 f<int>
。
考虑一下代码的和平:
template<class T>
void f(const T& t)
{
static int x = 0;
cout<<++x<<endl;
}
int main()
{
int j = 0;
const int i = 0;
f(5);
f(i);
f(j);
}
我已经调用了 3 种类型的函数。虽然 5 和 j 可以是同一个东西,只是 int,const int i 肯定是不同的类型。
但无论如何我的输出是:
1
2
3
这意味着编译器为不同的类型实例化相同的函数。
我对么?谁能解释一下为什么?
此处 f
将针对类型 int
实例化一次,因为所有 3 次调用都只调用 f<int>
.
只要类型推导有效就正确了。
考虑一下:
#include <iostream>
template<class T>
void f(const T& t)
{
static int x = 0;
std::cout<< "x=" << ++x<<std::endl;
}
template<class T>
void f(T&& t)
{
static int y = 0;
std::cout<< "y=" << ++y<<std::endl;
}
template<class T>
void f(T& t)
{
static int z = 0;
std::cout<< "z=" << ++z<<std::endl;
}
int main()
{
int j = 0;
const int i = 0;
f(5);
f(i);
f(j);
}
输出:
y=1
x=1
z=1
来自[temp.deduct.call]:
Template argument deduction is done by comparing each function template parameter type (call it P) with the type of the corresponding argument of the call (call it A) as described below.
P
是const T&
,A
是int
,int
,const int
三个调用。
然后我们有:
If P is a reference type, the type referred to by P is used for type deduction.
P
是引用类型,所以我们用P' == const T
来推导A == int
或A == const int
。在这两种情况下,我们都推导 T == int
以便 P' == const int
(和 P == const int&
)和推导的 A == const int
。这比前两次调用的原始 A
更 cv 合格,但明确表示可以:
In general, the deduction process attempts to find template argument values that will make the deduced A identical to A (after the type A is transformed as described above). However, there are three cases that allow a difference:
— If the original P is a reference type, the deduced A (i.e., the type referred to by the reference) can be more cv-qualified than the transformed A.
因此,所有三种情况都只调用 f<int>
。