-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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I use references very often but is there any significant difference to pass the primitive types by reference?
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?

5 Comments

could you please elaborate more on why it is better?
Why not a constexpr std::string then?
@YSC, your compiler will puke on that. string_view was added in C++17 to help.
@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.
True. Maybe a std::string_view then. At that point, I'm starting to agree with you with the use of char[].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.