Re: Clang does not like constexpression

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

 




Here's a C++11 version that might be useful to you (largely untested,
and I'm only on my first cup of coffee, so... ;>).

-Jesse
// C++11 constexpr string length:

#include <iostream>
#include <cstring>

size_t constexpr string_length(const char *s)
{
 return 0 == *s ? 0 : 1 + string_length(1 + s);
}

int main()
{

 {
 constexpr const char *s = "Howdy!";
 constexpr auto len = string_length(s);
 auto runtime_len = strlen(s);
 std::cout << len << ", " << runtime_len << '\n';
 }

 {
 constexpr const char *s = "";
 constexpr auto len = string_length(s);
 auto runtime_len = strlen(s);
 std::cout << len << ", " << runtime_len << '\n';
 }
 
 // ...note that trying to pass in a nullptr is not allowed in
 // a constexpr. 
}

[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