CTFE attribute

Vladimir Panteleev vladimir at thecybershadow.net
Sat Jan 28 11:44:07 PST 2012


On Saturday, 28 January 2012 at 19:02:56 UTC, Manu wrote:
> I still don't want it polluting my lib, and I also don't want 
> the possibility that someone COULD link to it. It's simply not 
> runtime code. Additionally, while the code is still present in 
> my lib, so must be the import that it makes use of...
>
> I'd also like an error if the function were called outside of 
> CTFE... so the CTFE attribute others have discussed seems like 
> a good idea to me.

You could make it private, and expose it via a template.

template foo(ARGS...)
{
    enum foo = fooImpl!(ARGS)();
}

private auto fooImpl(ARGS...)()
{
    // ...
}

Phobos uses this idiom often, but mainly because CTFE is more 
flexible that template metaprogramming.

Additionally, simply making the function templated would prevent 
it from ending up in your library's object file. You can make any 
regular function templated with an empty template parameter list:

void foo()(...) { ... }



More information about the Digitalmars-d mailing list