dimanche 3 avril 2016

strlen() compile time optimization

Few days ago I discovered that you can find compile-time strlen using something like this:

template<size_t N>
constexpr size_t strlen_(const char (&data)[N]) noexcept{
    return N - 1;
}

If it's compiled, then all is good. You can add overload such this:

size_t strlen_(const char *s) noexcept{
    return strlen(s);
}

Then it will always compiles.

My question is - does C++ <cstring> uses something like this and if it does not - why?

Aucun commentaire:

Enregistrer un commentaire