Structures and CTFE

Alex sascha.orlov at gmail.com
Mon Sep 3 15:00:33 UTC 2018


On Monday, 3 September 2018 at 14:00:23 UTC, agorkvmh wrote:
> There is a way to do print the two values at compile time?

Yes. Put a pragma where you static assert for Foo(1).pos equality 
with 2:

--
static assert(Foo(1).pos == 2);
pragma(msg, Foo(1).pos);

struct Foo
{
     this(int i)
	{
		static assert(this.init.pos == 1);
		advance();
	}

     size_t pos = 1;

     void advance()
     {
         pragma(msg, pos);
         pos = pos + 1;
         pragma(msg, pos);
     }
}

void main(){}
--
> What's the best way to debug a CTFE function?

There is none.

 From the Dlang tour:
"CTFE is a mechanism which allows the compiler to execute 
functions at compile time. There is no special set of the D 
language necessary to use this feature - whenever a function just 
depends on compile time known values the D compiler might decide 
to interpret it during compilation."
https://tour.dlang.org/tour/en/gems/compile-time-function-evaluation-ctfe

As no specifics for CTFE writing exist, no specifics for CTFE 
testing exist.

So, the way to choose is the one you did: by putting static 
asserts at the places where you want to assert, that something 
has to have specific values.
I also added another one, before the initialization of the 
struct. I.e., before calling the advance function.


More information about the Digitalmars-d-learn mailing list