admin管理员组文章数量:1395730
I'm trying to figure out how C++ actually breaks down the argument-passing when the formal argument is a reference and the actual argument is of a type that requires type-conversion.
- Here's an example. Are the calls to
f(10)
andg(5)
supposed to compile/run or they supposed to throw an error?
f(10)
passes an int but the formal-argument is a reference to const
. So the compiler has to bind the reference to 10. How does it do that? What assignment statement does it create to bind it? would it create a temporary object (say tmp
) and insert a statement like A tmp(10); f(tmp);
instead of call to f(10)
?
class A {
public:
A(int);
~A() = default;
};
void f(const A&);
void g(A&);
f(10);
g(5);
Can someone elaborate on what type-conversion and argument-passing rules apply here?
When running
g(5)
call I get the error:error: cannot bind non-const lvalue reference of type ‘A&’ to an rvalue of type ‘A’
Can someone explain the reasoning behind this?
I'm trying to figure out how C++ actually breaks down the argument-passing when the formal argument is a reference and the actual argument is of a type that requires type-conversion.
- Here's an example. Are the calls to
f(10)
andg(5)
supposed to compile/run or they supposed to throw an error?
f(10)
passes an int but the formal-argument is a reference to const
. So the compiler has to bind the reference to 10. How does it do that? What assignment statement does it create to bind it? would it create a temporary object (say tmp
) and insert a statement like A tmp(10); f(tmp);
instead of call to f(10)
?
class A {
public:
A(int);
~A() = default;
};
void f(const A&);
void g(A&);
f(10);
g(5);
Can someone elaborate on what type-conversion and argument-passing rules apply here?
When running
g(5)
call I get the error:error: cannot bind non-const lvalue reference of type ‘A&’ to an rvalue of type ‘A’
Can someone explain the reasoning behind this?
1 Answer
Reset to default 2
f(10)
passes anint
but the formal-argument is a reference toconst
. So the compiler has to bind the reference to 10. How does it do that? What assignment statement does it create to bind it?
It can't bind to 10
directly. It can bind only to a valid A
object.
would it create a temporary object (say
tmp
) and insert a statement likeA tmp(10); f(tmp);
instead of call tof(10)
?
Essentially, yes. A const reference can bind to a temporary, so that is exactly what the compiler does - it implicitly creates an unnamed temporary A
object, constructed using 10
as input to its constructor, then passes a reference to that object into f()
, and then destroys that object after the full statement that created it is finished (ie in your example, when the ;
is reached).
When running
g(5)
call I get the errorerror: cannot bind non-const lvalue reference of type ‘A&’ to an rvalue of type ‘A’
. Can someone explain the reasoning behind this?
It is because a non-const reference is not allowed to bind to a temporary, so you will have to create a non-temporary A
object yourself and then pass it to g()
explicitly.
本文标签:
版权声明:本文标题:reference - How *actually* does this type-conversion argument-passing happen in this function call in C++ - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744118621a2591629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
A
can be constructed from aint
it can make the call tof(10)
by converting the integer to an instance ofA
and then pass that temporaryA
byconst
reference to the function. Callingg(5)
doesn't work since you can't pass a temporaryA
by non-const
reference. – Jesper Juhl Commented Mar 27 at 0:43f(10)
tof(A{10})
. It's not quite the same, since the latter could do an implicit conversion fromA
to something else, but it's close enough. – Miles Budnek Commented Mar 27 at 0:56f(const A&)
and pass aA(5)
to it)? Why do you keep talking about the non-existent and unrelated "assignment" or "statement"? The "reference initialization" page on cppreference may help. Also for C++17 and later, read about temporary materialization. – Weijun Zhou Commented Mar 27 at 3:10