Why isn't opMul allowed in a const expression

Steven Schveighoffer schveiguy at yahoo.com
Tue Nov 20 06:59:56 PST 2007


For example:
struct X
{
  int value;
  X opMul(int y)
  {
    return X(y * value);
  }
}

void main()
{
  const X myX = X(4); // OK
  const X myX2 = X(4 * myX.value); // OK
  const X myX3 = myX * 4; // Error
}

On dmd 1.023 I get:
Error: non-constant expression ((X(4)).opMul)(4)

On dmd 2.006, after adding a const qualifier to opMul, I get:
Error: non-constant expression cast(const X)((X(4)).opMul)(4)

This is somewhat discouraging, as I cannot use my nifty operator overloads 
to generate constant value type structs.

Is this a bug?

-Steve 





More information about the Digitalmars-d mailing list