Why isn't opMul allowed in a const expression
Steven Schveighoffer
schveiguy at yahoo.com
Tue Nov 20 22:36:29 PST 2007
"torhu" wrote
> Steven Schveighoffer wrote:
>> I added:
>>
>> static X tmpOpMul(X x, int y)
>> {
>> return X(x.value * y);
>> }
>>
>> To X, and I can do:
>>
>> static myX4 = X.tmpOpMul(myX, 4);
>>
>> without error. I don't see how this is any different than calling opMul.
>> It should be evaluatable at compile time, no?
>>
>
> For initializing a constant you need a compile-time value.
>
> From the docs about compile-time functions:
> "4. the function may not be a non-static member, i.e. it may not have a
> this pointer"
Yep, you are right.
There needs to be a change here. Either:
1. I need to be able to specify opX as a static function, where opX is a
binary math operator.
2. I need to be able to use struct member functions as long as they adhere
to the other rules of CTFE.
I don't think this request is unreasonable. Structs are different from
classes in that all member functions are final. So there is no question as
to calling a virtual method, or how to construct a struct object to call the
method with.
Without something of this nature, I can't create a true value type.
Walter, is something like this being planned?
-Steve
More information about the Digitalmars-d
mailing list