Re: Clang does not like constexpression

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Jun 14, 2017 at 7:59 PM, Adam C. Emerson <aemerson@xxxxxxxxxx> wrote:
> #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;
>   }
> }

May we put this somewhere under common/? Although the mentioned
Clang build failure has been resolved this morning, I bet the constexpr-
capable variant of strlen (ceph::strlen?) will be very useful in the future.
Also, I would love to make a clean-up PR based on it.

Regards,
Radek
--
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



[Index of Archives]     [CEPH Users]     [Ceph Large]     [Information on CEPH]     [Linux BTRFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux