Overload resolution (value vs reference)

m0rph m0rph.mailbox at gmail.com
Sun Oct 21 10:01:48 PDT 2012


How does compiler selects the proper function among overloaded
functions which differ only in the way the argument is passed (by
reference or by value)? Is there a way, to control this behavior?

Result of execution of the test code:
passed by value: 8
passed by value: 3
passed by value: Foo(10)


Test code:

import std.stdio;

struct Foo {
      int value;
}

void f(T)(T foo)
{
      writeln("passed by value: ", foo);
}

void f(T)(const ref T foo)
{
      writeln("passed by reference: ", foo);
}

void main()
{
      // 8 is a r-vlaue, so it's passed by value
      f(8);

      // i is a l-value, it's passed by value and it's ok
      int i = 3;
      f(i);

      // foo is a l-value, it's passed by value again, but if
structure will be big enough it'll be ineffective

      auto foo = Foo();
      foo.value = 10;
      f(foo);
}



More information about the Digitalmars-d-learn mailing list