Std :: string vs char array for static constant
I want to use a string constant in multiple places in my cpp file. Should I use std :: string or char []?
static const std::string kConstantString = "ConstantStringValue";
static const char kConstantString[] = "ConstantStringValue";
I was told to prefer the latter as it "avoids static allocation". Shouldn't you also statically allocate the char array?
source to share
Yes, yes, it must also be statically allocated.
Always use std::string
unless your profiler tells you to write with legacy shit like const char[]
. Choice const char[]
is an annoying micro-optimization versus std::string
and a silly decision unless you know for sure that this piece of code is a hotspot (since it's static anyway, I highly doubt it).
source to share