February 27

On Wednesday, 26 February 2025 at 19:34:24 UTC, bkoie wrote:

>

On Wednesday, 26 February 2025 at 16:38:20 UTC, Salih Dincer wrote:

>

My_Static_Struct!ubyte(41, [0, 0, 0, 0, 0, 0, 0, 0])
My_Static_Struct!ubyte(42, [0, 0, 0, 0, 0, 0, 0, 0])
My_Dynamic_Struct!ubyte(41, [16, 32, 216, 47])
My_Dynamic_Struct!ubyte(42, [32, 32, 216, 47, 100, 127, 0, 0])
*/

code doing a bunch of undefined runtime magic
if you think this is valid then you will enjoy c++.

I hate CPP. If we had done something similar by relying on him, we would probably have struggled too hard. Cheers with :D

AI (DeepSeek) generated the following code that did not work in response:

#include <array>
#include <vector>
#include <memory>
#include <iostream>

template <typename Type>
struct My_Static_Struct {
    Type id;
    std::array<Type, 8> arr;

    My_Static_Struct() = delete;

    static My_Static_Struct init(Type id_val) {
        return {id_val, {}};
    }
};

template <typename Type>
struct My_Dynamic_Struct {
    Type id;
    std::unique_ptr<Type[]> arr;
    size_t arr_length;

    My_Dynamic_Struct() = delete;

    static My_Dynamic_Struct init(Type id_val, size_t length) {
        return {id_val, std::unique_ptr<Type[]>(new Type[length]), length};
    }
};

int main() {
    using T = unsigned char;

    // My_Static_Struct kullanımı
    using MSS = My_Static_Struct<T>;
    std::vector<MSS> m1 = {
        MSS::init(41),
        MSS::init(42)
    };

    for (const auto& elem : m1) {
        std::cout << "MSS id: " << static_cast<int>(elem.id) << " | arr: ";
        for (auto val : elem.arr) {
            std::cout << static_cast<int>(val) << " ";
        }
        std::cout << std::endl;
    }

    // My_Dynamic_Struct kullanımı
    using MDS = My_Dynamic_Struct<T>;
    std::vector<MDS> m2 = {
        MDS::init(41, 4),
        MDS::init(42, 8)
    };

    for (const auto& elem : m2) {
        std::cout << "MDS id: " << static_cast<int>(elem.id) << " | arr: ";
        for (size_t i = 0; i < elem.arr_length; ++i) {
            std::cout << static_cast<int>(elem.arr[i]) << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}

SDB@79

February 27

On Thursday, 27 February 2025 at 02:09:29 UTC, Arredondo wrote:

>

On Thursday, 27 February 2025 at 01:22:07 UTC, Hipreme wrote:

>

And no, it won't create 2 copies. D has a thing called Return Value Optimization. Since that local variable is returned, it has no need to copy it.

That's great to know. Thank you!

template Model(T, T n = 100) // where n: length with Type of T
{
    auto voidStackArr(inout bool _void = true)
    {
        T[n] arr = void;
        assert(!_void);
        arr = 0;
        return arr;
    }
}

alias voidStackArrInt = Model!int;
void main()
{
    import std.stdio: writeln;

    // err we are reading garb values
    auto arr = voidStackArrInt.voidStackArr;
    arr.writeln;

    // ok
    auto arr2 = voidStackArrInt.voidStackArr(false);
    arr2.writeln;
}

and we're back to c++ u guys should really stop doing things like this

February 27

On Thursday, 27 February 2025 at 06:15:38 UTC, bkoie wrote:

>

template Model(T, T n = 100) // where n: length with Type of T

spoiler alert: there is fun to be had here; your going to want to clear your sleep sch, stock up on caffine, digging into this syntax is a wild ride of some of the best compiler bugs d can offer 10/10

1 2
Next ›   Last »