On 14/06/2017, Radoslaw Zarzynski wrote: > This isn't a drop-in replacement for strlen(). It works differently > for strings with \0 buried somewhere in the middle. #include <iostream> #include <cstdlib> #include <stdexcept> namespace _ { constexpr std::size_t string_length_(const char* s, std::size_t i, std::size_t n) { return i < n ? (*(s + i) == '\0' ? i : string_length_(s, i + 1, n)) : throw std::invalid_argument("Unterminated string constant."); } } template<std::size_t N> constexpr std::size_t string_length(const char(&s)[N]) { return _::string_length_(s, 0, N); } constexpr const char s1[] = "cat"; constexpr const char s2[] = "cat\0dog"; constexpr const char s3[3] = { 'c', 'a', 't' }; constexpr std::size_t n1 = string_length(s1); constexpr std::size_t n2 = string_length(s2); int main(void) { std::cout << "string_length(s1): " << n1 << std::endl; std::cout << "string_length(s2): " << n2 << std::endl; try { std::cout << "string_length(s3): " << string_length(s3) << std::endl; } catch (const std::invalid_argument& e) { std::cerr << "How exceptional! " << e.what() << std::endl; } } -- To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html