CT BitArray

Johan j at j.nl
Fri Apr 3 19:13:13 UTC 2020


On Thursday, 2 April 2020 at 12:41:28 UTC, Steven Schveighoffer 
wrote:
> On 4/2/20 8:26 AM, Bastiaan Veelo wrote:
>> On Thursday, 1 November 2018 at 08:50:38 UTC, Bastiaan Veelo 
>> wrote:
>>> On Thursday, 1 November 2018 at 00:01:04 UTC, Stefan Koch 
>>> wrote:
>>>> On Wednesday, 31 October 2018 at 23:14:08 UTC, Bastiaan 
>>>> Veelo wrote:
>>>>> Currently, BitArray is not usable at compile time, so you 
>>>>> cannot do
>>>>> ```
>>>>> enum e = BitArray([1, 1, 1, 0]);
>>>>> ```
>>>>> This gives
>>>>>> /dlang/dmd/linux/bin64/../../src/phobos/std/bitmanip.d(1190): Error: `bts` cannot be interpreted at compile time, because it has no available source code
>> 
>
> Hm... I thought there was precedent for providing fallback 
> implementations for intrinsics. That is, you define the 
> function, which is only used if the intrinsic is not available.
>
> I can't remember where I saw this. But you could try this by 
> simply implementing the bitops in core.bitop, and see if they 
> are used outside ctfe.

There are a bunch of functions implemented with `if (!__ctfe)`. 
DMD and LDC are smart enough to elide `if(false/true)` control 
flow completely even in debug code, so there is no penalty to 
using `if (!__ctfe)`.
See for example: 
https://github.com/ldc-developers/druntime/blob/ldc/src/core/bitop.d#L85

-Johan





More information about the Digitalmars-d-learn mailing list