Say you want to obtain, at compile time, the length of a compile-time string constant. The problem is that the strlen
function is not constexpr
.
#include <cstring> template<std::size_t> void tryme(); void test() { // error; expression did not evaluate to a constant char buffer[strlen("hello") + 1]; // error: invalid template argument tryme<strlen("hello")>(); switch (0) { // error: case expression is not constant case strlen("hello"): break; } }
Note that gcc and clang support variable-length arrays as a nonstandard extension, so you may get away with the buffer
declaration unless you turn off that extension. In fact, gcc goes further and accepts all three statements!
How can you get all three of the above to work in standard-conforming code? One idea is to write your own constexpr_strlen
. But it turns out that somebody already wrote it for you, although it has a rather awkward name: std::
.
#include <string> constexpr std::size_t constexpr_strlen(const char* s) { return std::char_traits<char>::length(s); // or return std::string::traits_type::length(s); } constexpr std::size_t constexpr_wcslen(const wchar_t* s) { return std::char_traits<wchar_t>::length(s); // or return std::wstring::traits_type::length(s); }
in winnt.h:
//
// Return the number of elements in a statically sized array.
// DWORD Buffer[100];
// RTL_NUMBER_OF(Buffer) == 100
// This is also popularly known as: NUMBER_OF, ARRSIZE, _countof, NELEM, etc.
//
#define RTL_NUMBER_OF_V1(A) (sizeof(A)/sizeof((A)[0]))
I still would probably use sizeof(“wazzup”)-1 just because it is more readable.
As a side note one of my C/C++ gotcha questions to students is to ask what is the value of i.
But
sizeof("wazzup")
requires an embedded literal.The corresponding
sizeof(s) - 1
produces the wrong answer.constexpr auto a = std::string_view(“hello”).size();
Please next article “don’t use clang and gcc extensions with variable arrays along with coroutines”, i dont rly know why it is not ill-formed in clang
The problem with std::char_traits::length() is that it’s only constexpr since C++17 (at least according to https://en.cppreference.com/w/cpp/string/char_traits/length).
If C++11-to-14 compatibility is desired, there’s no way around something like
> In fact, gcc goes further and accepts all three statements!
As far as I can tell, gcc is smart enough to treat strlen as constexpr until told otherwise. (By telling it strlen is not an intrinsic.) This property also allows gcc to hoist strlen() invocations out of loop tests most of the time.
Older way of getting the length of a string at compile time: (sizeof("hello") - 1)
Wait isn't the first one just C99 variable length...