function not callable using struct constructor

Jonathan M Davis jmdavisProg at gmx.com
Thu Jan 2 19:59:22 PST 2014


On Friday, January 03, 2014 03:13:12 alex burton wrote:
> struct Foo
> {
> };
> 
> void bar(ref Foo f)
> {
> }
> 
> void main()
> {
> bar(Foo()); //Error: function test.bar (ref Foo f) is not
> callable using argument types (Foo)
> }
> 
> I get the above error with 2.064 not with 2.060.
> Is it a bug ?
> 
> Is it a feature ?
> If so :
> Why can't I take a non const ref to a temp struct - It might look
> a bit silly but I might be doing it to avoid copying.
> 
> I could still do this :
> 
> void main()
> {
> Foo f = Foo();
> bar(f);
> }
> 
> Which is equivalent AFAIK

ref parameters only accept lvalues. Foo() creates a temporary and is not an 
lvalue. If you want a function to accept rvalues, it has to not take its 
argument by ref. If you want to avoid copying lvalues and still accept 
rvalues, then you need to overload the function (one which takes ref and one 
which doesn't). And unlike in C++, constness has no effect on whether ref 
accepts rvalues. ref only ever accepts lvalues.

However, in the case of rvalues, ref doesn't save you anything performance-
wise. No copy will occur. Rather, because it's an rvalue, it can just move it 
into the function's parameter rather than copying it. ref only saves you 
copying with lvalues.

- Jonathan M Davis


More information about the Digitalmars-d mailing list