Structs and CTFE
Jonathan M Davis
jmdavisprog at gmail.com
Sun Jul 4 00:29:45 PDT 2010
Okay, as I understand it, you can't yet use classes with CTFE. As TDPL puts it:
"Alocating class objects and memory in general is not allowed." But my question
is "What about structs?"
I'm trying to create an enum of a struct type. The compiler insists that I
create an opCmp() for the struct (which makes sense, I suppose, since normally
enums are compareable), but even with opCmp() defined, the compiler appears to be
complaining about my constructors as well as opCmp(), saying that they can't be
evaluated at compile time.
For instance, if I were to write a program that looks like this
struct S
{
this(string phrase, int num)
{
this.phrase = phrase;
this.num = num;
}
int opCmp(const ref S rhs)
{
int phraseCmp = phrase.opCmp(rhs.phrase);
if(phrase != 0)
return phraseCmp;
if(num < rhs.num)
return -1;
else if(num > rhs.num)
return 1;
return 0;
}
string phrase;
int num;
}
enum E : S {a = S("hello", 1),
b = S("goodbye", 45),
c = S("world", 22)};
void main()
{
then I get the following errors
t.d(28): Error: variable __ctmp1 cannot be read at compile time
t.d(28): Error: cannot evaluate __ctmp1.this("hello",1) at compile time
t.d(28): Error: cannot evaluate __ctmp1.this("hello",1) at compile time
t.d(29): Error: cannot evaluate __ctmp2.this("goodbye",45) at compile time
t.d(29): Error: cannot evaluate __ctmp2.this("goodbye",45) at compile time
t.d(28): Error: cast(const(S))__ctmp1.this("hello",1) is not an lvalue
t.d(29): Error: cannot evaluate __ctmp2.this("goodbye",45) at compile time
t.d(29): Error: cannot evaluate
__ctmp2.this("goodbye",45).opCmp(cast(const(S))__ctmp1.this("hello",1)) at
compile time
t.d(29): Error: Integer constant expression expected instead of
__ctmp2.this("goodbye",45).opCmp(cast(const(S))__ctmp1.this("hello",1)) < 0
t.d(28): Error: cast(const(S))__ctmp1.this("hello",1) is not an lvalue
t.d(29): Error: cannot evaluate __ctmp2.this("goodbye",45) at compile time
t.d(29): Error: cannot evaluate
__ctmp2.this("goodbye",45).opCmp(cast(const(S))__ctmp1.this("hello",1)) at
compile time
t.d(29): Error: Integer constant expression expected instead of
__ctmp2.this("goodbye",45).opCmp(cast(const(S))__ctmp1.this("hello",1)) > 0
t.d(30): Error: cannot evaluate __ctmp3.this("world",22) at compile time
t.d(30): Error: cannot evaluate __ctmp3.this("world",22) at compile time
t.d(28): Error: cast(const(S))__ctmp1.this("hello",1) is not an lvalue
t.d(30): Error: cannot evaluate __ctmp3.this("world",22) at compile time
t.d(30): Error: cannot evaluate
__ctmp3.this("world",22).opCmp(cast(const(S))__ctmp1.this("hello",1)) at compile
time
t.d(30): Error: Integer constant expression expected instead of
__ctmp3.this("world",22).opCmp(cast(const(S))__ctmp1.this("hello",1)) < 0
t.d(28): Error: cast(const(S))__ctmp1.this("hello",1) is not an lvalue
t.d(30): Error: cannot evaluate __ctmp3.this("world",22) at compile time
t.d(30): Error: cannot evaluate
__ctmp3.this("world",22).opCmp(cast(const(S))__ctmp1.this("hello",1)) at compile
time
t.d(30): Error: Integer constant expression expected instead of
__ctmp3.this("world",22).opCmp(cast(const(S))__ctmp1.this("hello",1)) > 0
Is it not presently possible to use structs for enums, or to use them in CTFE in
general? Or am I just doing something wrong?
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list