-2

I have few questions about pre-processor macros. Here is an example of 2 different code pieces to talk about;

tmp.h
#define DEFAULT_STRING "default_value"

tmp.cpp
void tmpFunc(std::string newValue){
    m_stringValue = newValue;
    if(newValue.isEmpty())
        newValue = DEFAULT_STRING; 
}

And the second version

tmp.h
const std::string m_defaultValue = "default_value"

tmp.cpp
void tmpFunc(std::string newValue){
    m_stringValue = newValue;
    if(newValue.isEmpty())
        newValue = m_defaultValue; 
}

So my questions are;

  • Does the first version increase the binary size (comparing the first second)?
  • Does the second version consume more memory (comparing the first one)?
  • What are the performance impacts of both implementations? Which should run faster and why?
  • Which is preferable/beneficial in which condition?
  • Is there any better way for implementation of setting the default value?

Thanks.

2 Answers2

3

Does the first version increase the binary size (comparing the first second)?

No. Since the string is used only once, it will appear only once in the compiled file.

Does the second version consume more memory (comparing the first one)?

No. It works with memory in pretty much the same manner.

What are the performance impacts of both implementations? Which should run faster and why?

Both implementations copy the string around: the method receives newValue by value, copying it. This copying will dominate the performance, I guess.

However, as always with performance, just measure it - it should be easy, since you already have both versions of the code implemented.

Which is preferable/beneficial in which condition?

Macros are not an idiomatic solution in C++. Use the const string.

Is there any better way for implementation of setting the default value?

Receive the new string by reference. Other than that, not so much. I assume here the default is a short string, like "none" or "default". These strings have short string optimization, so it doesn't matter much how you store it.

As always with performance, if you see that your method is a performance bottleneck, optimize it. If you don't know, don't optimize it.

anatolyg
  • 23,079
  • 7
  • 51
  • 113
2

Both are horrible, the second one marginally better than the first.

The best way is to use

constexpr char DEFAULT_STRING[] = "default_value";

For more on constexpr, see When should you use constexpr capability in C++11?

Bathsheba
  • 220,365
  • 33
  • 331
  • 451
  • 6
    could you please elaborate more on why it is better? – Radim Bača Apr 16 '18 at 09:41
  • Why not a `constexpr std::string` then? – YSC Apr 16 '18 at 09:45
  • @YSC, your compiler will puke on that. `string_view` was added in C++17 to help. – Bathsheba Apr 16 '18 at 09:45
  • @YSC because `std::basic_string` does not have constexpr constructors. In idea the `constexpr std::string_view foo("abc");` should work but ... does not on majority of compilers yet. So char array is fine at the moment. – Öö Tiib Apr 16 '18 at 09:51
  • True. Maybe a [`std::string_view`](http://coliru.stacked-crooked.com/a/95358d7f60a7c24d) then. At that point, I'm starting to agree with you with the use of `char[]`. – YSC Apr 16 '18 at 09:52