I used constexpr to compute hash codes at compile time. The code compiles correctly, runs correctly. But I do not know if the hash values are compilation time or runtime. If I spend code at runtime, I do not execute constexpr functions. But they are not even tracked for runtime values (calculate the hash for the generated runtime string are the same methods). I tried to make out the disassembly, but I do not understand it at all.
For debugging purposes, my hash code is just a string length using this:
constexpr inline size_t StringLengthCExpr(const char * const str) noexcept { return (*str == 0) ? 0 : StringLengthCExpr(str + 1) + 1; };
I have an ID class created this way
class StringID { public: constexpr StringID(const char * key); private: const unsigned int hashID; } constexpr inline StringID::StringID(const char * key) : hashID(StringLengthCExpr(key)) { }
If I do this in the main method program
StringID id("hello world");
I got this parsed code (part of it - basically there are many other methods and other things)
;;; StringID id("hello world"); lea eax, DWORD PTR [-76+ebp] lea edx, DWORD PTR [id.14876.0] mov edi, eax mov esi, edx mov ecx, 4 mov eax, ecx shr ecx, 2 rep movsd mov ecx, eax and ecx, 3 rep movsb
How can I say from this that the "hash value" is compilation time. I do not see a constant like 11 to register. I am not very good at ASM, so maybe that’s right, but I’m not sure what to check or how to be sure that the values of the “hash code” are compilation time and are not calculated at runtime from this code.
(I am using Visual Studio 2013 + Intel C ++ 15 Compiler - VS Compiler does not support constexpr)
Edit:
If I change my code and do it
const int ix = StringLengthCExpr("hello world"); mov DWORD PTR [-24+ebp], 11 ;55.15
I have the correct result
Even so
change personal hashID to public
StringID id("hello world"); // mov DWORD PTR [-24+ebp], 11 ;55.15 printf("%i", id.hashID); // some other ASM code
But if I use private hashID and add getter
inline uint32 GetHashID() const { return this->hashID; };
for class id then i got
StringID id("hello world"); //see original "wrong" ASM code printf("%i", id.GetHashID()); // some other ASM code