Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get string length function (strlen) of a constant char array is not a constant expression

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?

like image 692
SLN Avatar asked Feb 09 '18 17:02

SLN


1 Answers

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;
}
like image 127
erenon Avatar answered Sep 22 '22 10:09

erenon