1

I'd like to implement a C-style sizeof() function in D. (I know about the .sizeof thing, but it's to help in porting a lot of C (C99).)

I want it to be able to run at compile-time, obviously and take a type or an expression as an argument. Ideally, I'd like it to have the same syntax as C, if at all possible, rather than use the distinctive template invocation syntax, as that would greatly increase its utility. Is this at all possible?

Cecil Ward
  • 597
  • 2
  • 13
  • size_t sizeof( T )( T x ) { return T.sizeof; } // is an abject failure - my first, doomed attempt – Cecil Ward Aug 19 '16 at 00:43
  • size_t sizeof( T )() { return T.sizeof; } size_t sizeof( T )( T x) { return T.sizeof; } // So’s this. – Cecil Ward Aug 19 '16 at 00:52
  • 1
    btw do you know this guide? http://dlang.org/ctod.html – greenify Aug 19 '16 at 01:59
  • Yes, I went right through that part of the site. I'm still very much a newcomer though. I'm currently re-reading Ali Çehreli's online book, extremely helpful. Shame the examples are a bit ropey and unrealistic, but there's only so much you can do in a tutorial when you need to maintain brevity and concentrate on the feature you're intending to showcase. – Cecil Ward Aug 19 '16 at 02:27
  • While it's still WIP, the DLang Tour also has some material to offer, e.g. http://tour.dlang.io/tour/en/gems/compile-time-function-evaluation-ctfe – greenify Aug 19 '16 at 07:30

1 Answers1

1

If I understand you correctly, you want a behavior similar to the size function below? size2 would be a runtime function, which of course is a bit pointless in D. However you can still get the value from size2 at CT with enum val = size2(2 + 2);. Does this help you?

template size(T)
{
    enum size = T.sizeof;
}

// for expressions
template size(alias T)
{
    enum size = T.sizeof;
}

auto size2(T)(T x)
{
    return T.sizeof;
}

void main(string[] args)
{
    import std.stdio : writeln;

    writeln(size!int); // 4
    writeln(size!long); // 8
    writeln(size!(1 + 1)); // 4


    writeln(size2(2));  // 4
    writeln(size2(2L)); // 8
    writeln(size2(2 + 2)); // 4
}
greenify
  • 1,127
  • 8
  • 7
  • Thank you. It's certainly a worthwhile advancement. I would still need to try getting rid of the ! where it's required for invocation. Second thing is, is sizeof a reserved word in D? Could I actually call a function or template simply "sizeof"? Not essential, as when porting C a global search-and-replace of sizeof( with size2( or whatever is not too onerous. – Cecil Ward Aug 19 '16 at 01:15
  • I'd like to mark this as "an answer", but I'm not even sure an answer to my wish for the moon on a stick actually exists. Well, to be far an authoritative "no way" is definitely an answer. Perhaps I should simply bribe Walter Bright! :-) – Cecil Ward Aug 19 '16 at 01:18
  • Is your function really a non-compile-time function? – Cecil Ward Aug 19 '16 at 01:18
  • I'll play around with your excellent suggestion later on when my hands are not hurting quite so badly. – Cecil Ward Aug 19 '16 at 01:21
  • > Second thing is, is sizeof a reserved word in D? Could I actually call a function or template simply "sizeof"? No, I just picked `size2` to make it clearer. – greenify Aug 19 '16 at 01:49
  • does anyone think that sizeof(uint), say, is doable, or impossible? Votes one way or the other? (As greenify has an ideal solution in size2() for expressions, I'm just lacking a solution for an argument that is a typename.) – Cecil Ward Aug 19 '16 at 01:53
  • > Is your function really a non-compile-time function? `size2` is a templated function as the the ones you were proposing (it's exactly the same, btw). D will infer T at CT, but _not_ execute the function, except one specifically ask for it. As said I am still not sure what whether this is what you are looking for. – greenify Aug 19 '16 at 01:55
  • btw if you already think about applying regexps, why not directly for `sizeof(uint)` -> `uint.sizeof`? – greenify Aug 19 '16 at 01:59
  • Good point about regex. That's where I'll have to go if plan A fails, no doubt. I still like the idea of bribing Walter bright. This is a silly incompatibility with C, where C wasn't broken and having to require your brain/fingers is painful for no benefit. – Cecil Ward Aug 19 '16 at 02:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121321/discussion-between-cecil-ward-and-greenify). – Cecil Ward Aug 19 '16 at 08:35