4 ms·
Let's try this: constexpr std::vector<int> f() { return {1,2,3}; } constinit std::vector<int> p = f(); // error in D! const(int)[] f() { return [
by WalterBright 6mo ago
Let's try this:
constexpr std::vector<int> f() { return {1,2,3}; }
constinit std::vector<int> p = f(); // error
in D!
const(int)[] f() { return [1,2,3]; }
immutable int[] aaa = f();
And the object file, look ma, aaa[] is statically allocated:
internal:
db 001h,000h,000h,000h,002h,000h,000h,000h ;........
db 003h,000h,000h,000h,000h,000h,000h,000h ;........
__D5test63aaayAi:
db 003h,000h,000h,000h,000h,000h,000h,000h ;........
db 000h,000h,000h,000h,000h,000h,000h,000h ;........
- tialaramex 6mo agoBut why though? What is the purpose of the immutable "dynamic" array in your D? We're unavoidably going to learn at compile time how big the array is, and knowing how big it is will always be the same or better, so we should always do that.
- WalterBright 6mo agoIf you know at compile time what the size should be: int[f().length] array; should do it.
- deleted 6mo ago[deleted]