I would like to initialise a struct member with a hash of the struct name.
constexpr uint32_t myHash(const char* const data)
{ //Some code for hash
return myHash;
}
struct My_Struct{
constexpr Test() : ID(myHash("My_Struct"))
{
}
const uint32_t ID;
}
When I have:
constexpr My_Struct my_constexpr_struct;
Then the hash is computed at compile time successfully. However, when I have in my main function
My_Struct my_normal_struct;
then it will call the
constexpr uint32_t myHash(const char* const data)
function in the code instead of simply initialising the struct member with a compile time constant.
This would obviously incur a significant performance penalty that is avoidable.
Any thoughts or suggestions on how to have the compiler perform this at compile time? I don't really want to do:
constexpr uint32_t MY_STRUCT_ID = myHash("My_Struct");
struct My_Struct{
constexpr Test() : ID(MY_STRUCT_ID)
{
}
const uint32_t ID;
Thanks.