"ref const" parameters in functions

L-MAN vangelisforever at yandex.ru
Sat Mar 31 14:25:51 PDT 2012


Hello everybody!

I'm trying to use some function FN like this:

struct X
{
  protected double _x;  // double type for example
  public @property double X() const { return _x; }

  // ctor
  public this(double x) { _x = x; } // double type for example

  void FN(ref const(double) in_x) // double type for example
  {
    // ... do some operations with _x
  }

}

main(..)
{
  ....
  X x = X(20);

  x.FN(30); // getting an error
}


  why x.FN(20) gets me an error?
  the construction "ref const(double)" or "ref immutable(double)" 
must be an rvalue by default I think, but in FN parameter the 
compiler expects an lvalue...
  this strategy is the way to some unnecessary copy operations, 
when value 20 (or some big struct instead of it) will copy to the 
stack..

How can I resolve this problem?

Thanks!


More information about the Digitalmars-d-learn mailing list