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.constexpr int tmplen(char const* prefix) { return std::char_traits::length(prefix) + 5; } void mktemp(char* buffer, char const* prefix) { strcpy(buffer, prefix); strcat(buffer, "temp"); } void test() { constexpr char const* prefix = "hello"; char buffer[tmplen(prefix)]; mktemp(buffer, prefix); }The corresponding
sizeof(s) - 1produces 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