Reputation: 21
I am developing cross-platform C++ library. I wanted a better way of defining my string constants which will be used throughout the program. Which one is better static const char * or static const std::string? Does constexpr make any of them better over the old declaration?
static const char *str = "hello world";
vs
static const string str = "hello world";
Why I need this: My program might have to initialise a lot of static variable of string literals type before starting app. And i can't get away with static variables because they are used in the entire file. If i have to increase the performance, but reducing time of static variable initialisation. Ideally i would want most of things to happen at compile time rather than runtime. which one would be better choice?
Upvotes: 0
Views: 2250
Reputation: 11168
Usually, std::string
is good but in any case it depends on what you want to do. There is no absolute silver bullet here. std::string
has a constructor and will call new/malloc
for example (when SSO small string optimization can't be applied). If this is undesirable for any reason, go with const char*.
It depends also on where it 's to be used. If this is a HWND window title for example and the main use is with Windows API functions, it won't differ if you use std::string's c_str() method all the time.
All this is to advise that there is no 100% preference for one or another.
The usage or not of static
is a different discussion.
Upvotes: 4
Reputation: 41780
It will depend on what you do with these variables. For example, take a look at this:
void do_stuff(std::string const&);
void stuff() {
while(/* some condition */) {
do_stuff(constant); // very hot loop
}
}
Let's take the two ways of defining the constant
variable:
constexpr auto constant = "value"; // (1)
auto const constant = std::string{"value"}; // (2)
In the case of the hot loop, if you take (1)
, it will create a string each time. If the string value it long enough, it can cause allocation. Then (2)
is better for that case.
On the other hand, if you refactor your code to something like this:
void do_stuff(std::string_view);
void stuff() {
while(/* some condition */) {
do_stuff(constant); // very hot loop
}
}
Then your program will be much faster with (1)
, since no dynamic allocation will occur. Your program will refer to the original read only data of your program.
Upvotes: 2
Reputation: 18864
With modern C++
I would use std::string_view
.
If you do need it to be std::string
then the best option is to define it as a static local variable in a function, so it is lazily initialized upon the first request.
/* inline or not */
std::string const& get_that_string() {
static std::string const str { "value" };
return value;
}
Upvotes: 0
Reputation: 206627
Unless there are known problems in your specific code base with using std::string
, I strongly suggest using std::string
.
static std::string const str = "hello world";
Upvotes: -1
Reputation: 83537
In general you should prefer std::string
over char*
. static
has nothing to do with it.
Upvotes: -2