dimanche 2 août 2020

User Defined Literals for a String versus for a Hex Value

Regarding this question, why does a a user defined literal for a hex value map to a different string literal operator than a string does? That is, why does the code

std::vector<uint8_t> val1 = 0x229597354972973aabbe7_hexvec;

map to

std::vector<uint8_t> operator"" _hexvec(const char*str)
{
    // Handles the form 0xFFaaBB_hexvec and 0Xf_hexvec
    size_t len = strlen(str);
    return convertHexToVec(str, len);   
}

while the code

std::vector<uint8_t> val2 = "229597354972973aabbe7"_hexvec;

maps to

std::vector<uint8_t> operator"" _hexvec(const char*str, std::size_t len)
{
    // Handles the conversion form "0xFFAABB"_hexvec or "12441AA"_hexvec
    return convertHexToVec(str, len);
}

What makes the size_t necessary when both are null terminal strings? For that matter, why is 0x551A_hexvec a string at all? Why not an integer?

Aucun commentaire:

Enregistrer un commentaire