How to ensure template function can be processed during compile time

Max Samukha maxsamukha at
Wed Jul 8 20:33:25 UTC 2020

On Wednesday, 8 July 2020 at 20:11:05 UTC, IGotD- wrote:

> Now since mySize is a template, shouldn't this work mySize!v, 
> but it doesn't? What essential understanding have I missed here?

You are trying to use a run-time value of v at compile-time, 
which is not possible. If you want the modified size of the type 
of a variable, you can pass the variable to the compile-time 
function by alias:

enum mySize(alias e) = e.sizeof + 42;

int v;
enum size = mySize!v;

pragma(msg, size); // 46LU

If you want to the size of the type of a run-time expression, you 
will have to use typeof:

static assert(mySize!(typeof(v + 1)) == 46);

More information about the Digitalmars-d-learn mailing list