Let's see the simplified code directly (compiled by: GCC 6.3.0)
#include<iostream>
#include<cstring>
using namespace std;
int main(int arga, char* argv[]) {
const char cs[] = "Hello";//define a constant c-style string
constexpr size_t newSize = strlen(cs) + strlen(" ");//Error
return 0;
}
Compiler yielded an error: strlen(((const char*)(& cs))) is not a constant expression
However, when I move the c-string definition to the global scope, then the problem is off.
....
const char cs[] = "Hello";
int main(int arga, char* argv[]) {
constexpr size_t newSize = strlen(cs) + strlen(" ")//No Error
....
}
Can someone explain what happened? Why strlen() sees a globally defined constant c-string as a constant expression, but not the one in the stack?
Standard strlen
is not constepxr
, therefore, it cannot be used in constexpr
context. However, GCC knows about strlen, therefore it is able to compute the length of the string, in some circumstances - even if it is not mandated/permitted by the standard.
If you are concerned only by arrays, you can use std::size to get their size:
template <class T, std::size_t N>
constexpr std::size_t size(const T (&array)[N]) noexcept
{
return N;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With