To address your question of how to return the integer values of a struct as an array using a function in C++, while allowing flexibility in the array element type (e.g., uint8_t, uint32_t, etc.), the most idiomatic and type-safe approach is to use templates in C++. This allows a single function to handle different array types without requiring separate functions or unsafe type conversions.
✅ Problem Summary
You have a struct like this:
struct HDInfo {
bool done;
uint8_t cleared;
uint8_t attack;
uint8_t b2b;
};
You want to write a function that fills an array with the integer values from this struct (cleared, attack, b2b), and you want the array to be of a flexible type — such as uint8_t, uint32_t, or even int, depending on the context.
✅ Solution: Use a Template Function
You can write a template function that accepts a pointer to an array of any type T, and fills it with the struct's integer values, converted to type T.
Here's how you can implement this:
#include <cstdint> // for uint8_t, uint32_t, etc.
#include <cstddef> // for size_t
struct HDInfo {
bool done;
uint8_t cleared;
uint8_t attack;
uint8_t b2b;
};
// Template function to fill array with struct values
template <typename T>
void FillArray(const HDInfo& info, T* outArray) {
outArray[0] = static_cast<T>(info.cleared);
outArray[1] = static_cast<T>(info.attack);
outArray[2] = static_cast<T>(info.b2b);
}
✅ Usage Example
You can now call this function with arrays of different types:
HDInfo info = {false, 2, 5, 7};
uint8_t arr_u8[3];
FillArray(info, arr_u8); // Works with uint8_t
uint32_t arr_u32[3];
FillArray(info, arr_u32); // Works with uint32_t
int arr_int[3];
FillArray(info, arr_int); // Works with int
Each of these calls will correctly convert the uint8_t struct members to the target type T.
🔍 Notes
- Type Safety: The
static_cast<T>() ensures that the conversion is explicit and safe for numeric types. This avoids warnings or errors due to implicit narrowing conversions.
- Array Size: The function assumes that
outArray has at least 3 elements. You must ensure this manually or add a size parameter for additional safety.
- Template Deduction: The compiler automatically deduces
T based on the type of the second argument (outArray), so no explicit template arguments are needed.
bool Handling: Since done is a bool, and the question focuses on integer values, it is not included in the array. If you want to include it, you can cast it as T(done) as well.
🧠 Alternative (Not Recommended)
You could technically use a void* and cast inside the function, but this approach is not type-safe and can lead to undefined behavior. It also requires passing the type separately, which is more complex and error-prone.
✅ Summary
To return the integer values of a struct as an array with flexible element types:
- Use a template function.
- Accept a pointer to an array of type
T.
- Use
static_cast<T> to convert each struct member to the desired type.
- Ensure the array has enough space for all values (in this case, 3).
This approach is clean, type-safe, and leverages C++'s powerful template system.