Re: How to ensure template function can be processed during compile time

2020-07-08 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 8 July 2020 at 20:11:05 UTC, IGotD- wrote: int v; enum sz = mySize!int // works, returns 46 enum sz2 = mySize(v) // doesn't work. Error: variable v cannot be read at compile time Here we have a difference between C++ and D as C++ was able infer the size of v during compile tim

Re: How to ensure template function can be processed during compile time

2020-07-08 Thread Max Samukha via Digitalmars-d-learn
On Wednesday, 8 July 2020 at 20:11:05 UTC, IGotD- wrote: Now since mySize is a template, shouldn't this work mySize!v, but it doesn't? What essential understanding have I missed here? You are trying to use a run-time value of v at compile-time, which is not possible. If you want the modifie

Re: How to ensure template function can be processed during compile time

2020-07-08 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Jul 08, 2020 at 08:11:05PM +, IGotD- via Digitalmars-d-learn wrote: [...] > Doing the same in D, would with my lack of knowledge look like this. > > > size_t mySize(T)() > { > return T.sizeof + 42; > } What you want is: enum mySize(T) = T.sizeof + 42; And there is no ne

How to ensure template function can be processed during compile time

2020-07-08 Thread IGotD- via Digitalmars-d-learn
I have the following functions in C++ template inline constexpr size_t mySize(const T &v) { return sizeof(v) + 42; } template inline constexpr size_t mySize() { return sizeof(T) + 42; } The constexpr ensures that it will be calculated to a compile time constant otherwise the build wil