Partially initialized structs?
Salih Dincer
salihdb at hotmail.com
Thu Feb 27 06:12:27 UTC 2025
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:
```CPP
#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 at 79
More information about the Digitalmars-d-learn
mailing list