compile time 'address'
Steven Schveighoffer
schveiguy at gmail.com
Thu Nov 29 18:49:27 UTC 2018
On 11/29/18 12:59 PM, Dominic Jones wrote:
> Hello Steve,
>
> Thank you for your comments. A couple of replies:
>
>
> On Thursday, 29 November 2018 at 16:29:00 UTC, Steven Schveighoffer wrote:
>> 1. No you cannot read c0 or c1 at compile time, because they are
>> runtime values. You can fix this by changing them to immutable or enum.
>
> The variables are deliberately 'auto' rather than 'auto constexpr' as
> per the C++ example, so I do wish to preserve that in the D example.
> Nevertheless, in C++ querying (at least to perform comparison) the
> sudo-address, whatever it is (perhaps, behind the scenes it is simply a
> stack frame offset), is permitted.
>
>
>> 2. D doesn't like you taking the address of compile-time values, but
>> the error message is really bad.
>>
>> If I do this:
>>
>> auto cmp(T, U)(const T t, const U u) // note, no ref
>> {
>> return t == u; // note, change to value not address
>> }
>
> Presumably, this is now simply comparing values held by the variables,
> rather than the sudo-address of the original variables as per the C++
> example?
Ah, ok. Essentially you are looking for the layout of the local variables.
One thing that is available for D is the compile-time property offsetof,
which gives the offset from the start of an aggregate that an item is.
for example:
struct S
{
int x;
bool y;
}
static assert(S.y.offsetof == 4);
So while this isn't available for function locals, the idea is similar.
But I don't think there's an equivalent to what you are able to do here
in C++.
I don't think it would be a huge stretch to apply this same property to
function locals, but it would require an enhancement request.
-Steve
More information about the Digitalmars-d
mailing list