c++cpplint

static/global variable not permitted in C++


I have defined a global variable in my C++ class as follows :

std::string VAR = "HELLO_WORLD";

But cpplint is telling me :

Static/global string variables are not permitted. [runtime/string] [4]

Do you have an idea why ?


Solution

  • For the avoidance of doubt the language itself permits this.

    Essentially though the static analyser you are using forbids this because std::string contains a constructor so the statement actually "does something".

    Therefore it needs to be inside a function, not at global scope.

    On the other hand,

    const char* VAR = "HELLO_WORLD";
    

    is emitted, since that's no more than an assignment of a read-only const char[] literal to an appropriate pointer.