Zig vs D generics

Paulo Pinto pjmlp at progtools.org
Mon Oct 10 14:18:52 UTC 2022


On Monday, 10 October 2022 at 10:23:44 UTC, Ola Fosheim Grøstad 
wrote:
> On Monday, 10 October 2022 at 09:55:32 UTC, Ola Fosheim Grøstad 
> wrote:
>> On Monday, 10 October 2022 at 07:30:38 UTC, Kagamin wrote:
>>> Eh? So can C++ support an allocator that works both at 
>>> compile time and at run time? I use such allocator in my D 
>>> code so it's ctfeable.
>>
>> No, only encapsulated types can hold memory. This is the right 
>> approach when you don't have garbage collection. If you allow 
>> manual compile time allocation you end up with something that 
>> doesn't scale in terms of debugging.
>>
>> Or you need a very advanced debugger.
>
> Argh, I couldn't make this work either in C++20. I thought 
> std::string("hello world) should be constexpr returnable. 
> Apparently not, unless I did something wrong. (That is a big 
> weakness, so I really hope I did something wrong. ;-)

I guess you did something wrong, :)

https://godbolt.org/z/qKPGvbd1W

```cpp
#include <string>
#include <iostream>

constexpr std::string hello() {
     return std::string("hello world");
}

int main() {
     static constexpr auto greeting = hello();

     std::cout << greeting << std::endl;
}
```


More information about the Digitalmars-d mailing list