I would like to have a template function that returns an object of whatever class with random values. I tried the function below but it doesn't work, because tab is read as a uint32_t* rather than a sequence of numberOfCalls values of type uint32_t.
std::random_device rd;
template <typename Type>
Type RandomObject(void)
{
constexpr auto numberOfCalls = (sizeof(Type) + sizeof(uint32_t) - 1u)/sizeof(uint32_t);
uin32_t tab[numberOfCalls];
for (auto i = 0u; i < numberOfCalls; ++i)
tab[i] = rd();
return reinterpret_cast<Type>(tab);
}
Here's an example of something I would like to do.
struct Test
{
uint64_t s;
uint64_t t;
uint32_t x;
uint32_t y;
uint32_t z;
};
int32_t main(void)
{
const auto foo = ::RandomObject<Test>();
// do things
return 0;
}
Of course I could simply implement RandomObject<Test> but I'd much prefer having one robust template function that works for everything.
Aucun commentaire:
Enregistrer un commentaire