无法将“ a”(类型为“ int”)转换为类型“ double& [英] cannot convert 'a' (type 'int') to type 'double&
问题描述
我正在关注一些C ++教程,令人沮丧的是,原始资料指出无法进行此特定的函数调用,但根本无法解释原因;
I'm following some C++ tutorials, and frustratingly, the source material states this particular function call cannot be made, but doesn't at all explain why;
template<typename T>
void Swap(T &a, T &b){
T temp;
temp = a;
a = b;
b = temp;
}
现在我创建一个显式模板函数实例并传入 a
和 b
;
So now I create an explicit template function instantiation and pass in a
and b
;
int a = 5;
double b = 10.3;
Swap<double>(a, b);
然后抛出以下编译器错误;
Which then throws the following compiler error;
无法将 a(类型为 int)转换为 double&
cannot convert 'a' (type 'int') to type 'double&'
我唯一的预感是这是因为规则规定不能将rvalue绑定到非const引用,但是,如果 T& a
只是一个别名,它本身不是右值,是吗?我的隐式转换正在创建一个临时文件,它不能绑定到 T& a
吗?
My only hunch is that this is because of the rule that states an rvalue can't be bound to a non-const reference, however, if T &a
is just an alias, it's not an rvalue itself, is it? My implicit cast is creating a temporary, which it can't bind to T &a
? Is that what's happening?
temp
是左值。它可能仅具有功能范围,但确实存在。那么,为什么不允许隐式类型转换加倍,然后将引用a分配给 temp
呢?意图很明显。我不太了解引用作为参数。如果 a
被分配给temp,这是一个左值,那么这是不允许的吗?
temp
is an lvalue. It may only have function scope, but it does exist. So why wouldn't it allow the implicit cast to double, then assign the reference a to temp
? The intention looks pretty clear. I don't quite understand references as parameters. If a
is getting assigned to temp, which is an lvalue, then wouldn't this be allowed?
编辑:
第二个例子;
int a = 5;
double &d = a; // invalid initialization of reference of type 'double&' from expression of type 'int'
int a = 5;
const double &d = a; //valid
推荐答案
直观地,交换 a
和 b
应该可以工作,因为编译器可以在 int
和<$之间进行转换c $ c> double 。不过,在实践中,请考虑编译器必须执行什么操作才能完成您要问的事情。
Intuitively, swapping a
and b
should work, because the compiler can convert between int
and double
. In practice, though, think about what the compiler has to do in order to do what you're asking.
您从模板开始:
template<typename T>
void Swap(T &a, T &b){
T temp = a;
a = b;
b = temp;
}
要实例化为 double
,编译器会创建类似以下的函数:
To instantiate it for double
, the compiler creates a function something like this:
void Swap(double& a, double& b) {
double temp = a;
a = b;
b = temp;
}
这些都是 reference 参数-它们指向实际在内存中的位置,而不是别名或副本。在后台,引用的行为类似于指针,因此,如果有帮助,您可以与此类似地考虑函数:
Those are reference parameters - they point to actual locations in memory, rather than being aliases or copies. Under the hood, a reference behaves like a pointer, so you can think of your function similarly to this, if it helps:
void Swap(double* a, double *b);
换句话说,交换
需要引用(指向两个内存双打的指针-内存的两个8字节部分(假设双精度占用8个字节)。作为聪明而直观的人,我们知道 Swap
的实现实际上并不需要 两个8字节的内存部分,但是这就是
In other words, Swap
needs references (pointers) to two in-memory doubles - two 8-byte sections of memory (assuming a double takes 8 bytes). As intelligent and intuitive humans, we know that Swap
's implementation doesn't really need two 8-byte sections of memory, but that's how it's declared, and so that's how the compiler enforces it.
要制作可以处理混合参数的Swap版本,您必须执行以下操作:
To make a version of Swap that can handle mixed arguments, you'd have to do something like this:
template<typename T1, typename T2>
void Swap(T1& a, T2& b) {
T1 tmp(a);
a = b;
b = tmp;
}
这篇关于无法将“ a”(类型为“ int”)转换为类型“ double&的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!