May 09, 2013
On Thursday, 9 May 2013 at 01:08:28 UTC, Meta wrote:
> I can't get the structs to be evaluated at compile time, so I guess that's a lost cause.

Well, structs can be evaluated at compile-time but can't be value template parameters. You can do a small trick in this case though:

http://dpaste.1azy.net/c2188c90

struct Test(int N)
{
	enum n = N;
	
	auto add(int i)()
	{
		return Test!(n + i)();
	}
	
	alias n this;
}

void main()
{
	import std.stdio;
	enum a = Test!5();
	enum b = Test!3();
	writeln(a.add!b());
}

Will this do?

But actually this feels more like a task for CTFE. You can write a normal opAdd that does bounds check at run-time and then add "if (!__ctfe) assert(0);" to guarantee it will never be used in real run-time.
May 09, 2013
> then add "if (!__ctfe) assert(0);" to guarantee it will never be used in real run-time.

I don't think I've ever seen __ctfe before. Is it specific to DMD?
May 09, 2013
On Thursday, 9 May 2013 at 12:35:29 UTC, Meta wrote:
>> then add "if (!__ctfe) assert(0);" to guarantee it will never be used in real run-time.
>
> I don't think I've ever seen __ctfe before. Is it specific to DMD?

It is documented here : http://dlang.org/function.html

"The __ctfe boolean pseudo-variable, which evaluates to true at compile time, but false at run time, can be used to provide an alternative execution path to avoid operations which are forbidden at compile time. Every usage of __ctfe is evaluated before code generation and therefore has no run-time cost, even if no optimizer is used"

So no, it is quite official and not specific to dmd.
1 2
Next ›   Last »