January 28, 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()(...) { ... }

January 28, 2012
On 1/28/2012 11:02 AM, Manu wrote:
> I'd also like an error if the function were called outside of CTFE...

if (!ctfe) assert(0);
January 28, 2012
On 1/28/2012 12:38 PM, Walter Bright wrote:
> On 1/28/2012 11:02 AM, Manu wrote:
>> I'd also like an error if the function were called outside of CTFE...
>
> if (!ctfe) assert(0);

Oops,

   if (!__ctfe) assert(0);
January 29, 2012
Am 28.01.2012, 16:42 Uhr, schrieb Trass3r <un@known.com>:

>> When I build my code, I notice that the CTFE functions, which are never
>> referenced in any runtime code, are still present in the object file.
>
> For now you can get rid of it with -L--gc-sections (or LTO).
> gdc also needs -ffunction-sections -fdata-sections.

I've recently fucked up my first executable with that. -L--gc-sections had the effect as if 'throw' was a no-op. In other words runtime exceptions are silently ignored. If you use it double-check that exceptions are still thrown!
January 30, 2012
On 28/01/12 19:39, Walter Bright wrote:
> On 1/28/2012 6:50 AM, Manu wrote:
>> I'd certainly like to be able to mark these functions CTFE to be sure
>> no runtime
>> code will ever be generated for them, and also, what can I do about CTFE
>> imports? I don't want that import in my binary...
>
> Being in your object file doesn't mean they make it into the binary.
> Optlink, for example, will remove unreferenced COMDATs.


The problem is how to prevent the moduleinfo from getting linked in. Though this is a general problem, not specific to CTFE.

One thing I have been considering is, if a template is only ever instantiated in CTFE (eg in a static if, or while defining an enum value), should it still get written into the obj file? I don't think it is ever necessary, and discarding it would save a lot of space and time.
1 2
Next ›   Last »