5

We are migrating some of our code from c++03 to c++14 and wherever there is some performance gain we want to use c++14 features.Now in one of our projects we are parsing csv based on column names and these column names are declared in one header file like this :

const string ITEM_NAME = "Item Name";
const string ITEM_ID = "Item Id";

There are hundred of constants like this so what I want to know is Is there any significant performace gain if i change above code to something like this :

constexpr string ITEM_NAME = "Item Name";
constexpr string ITEM_ID = "Item Id";

Also is there any limit on number of constexpr that we can have in our binary since they need to be stored in read only memory ?

Does compiler automatically optimize old c++03 code also and place const variables also in read only memory and this effort is not worth ?

4
  • 2
    Off topic: It looks like your team is polluting the global namespace with a using namespace std; in a header or at some extremely broad scope. Risky and will get you in much trouble some places. Commented Jul 9, 2016 at 0:41
  • 1
    no we have all these constants inside namespace csvconst Commented Jul 9, 2016 at 0:47
  • Ha! Teach me to use "is" instead of "may be". I spent too much time agonizing over the wrong verbiage before posting. I stand down. Commented Jul 9, 2016 at 0:52
  • Another off topic: std::string is not a literal type, so code like constexpr string ITEM_NAME = "Item Name"; cannot compile in the first place, so no need to worry about significant performance gain. Commented Sep 27, 2024 at 13:23

2 Answers 2

9

There is no advantage to using constexpr except that you are allowed to evaluate the expression in certain contexts where non-constexpr expressions cannot be evaluated.

You should just stick with const unless you have a specific reason why constexpr works and const does not work.

If you really need the variables in read-only memory, then neither const nor constexpr are any different. I would only expect that trivial types would reliably get placed in read-only memory, and std::string is certainly not a trivial type. ("Trivial type" is a technical term.)

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

2 Comments

Can you provide example where non-constexpr expressions cannot be evaluated
@Kapil compile-time operations like template specializing and array sizing. Nothing you would have been able to compile in c++03 with const, so you should be good without changes.
2

No, I would not expect there to be any performance gain.

The C++ language does not specify a maximum limit on the number of constexpr objects/methods. Any limits would be imposed by your compiler and/or operating system.

The C++ language does not specify whether the compiler places const variables into something that's called "read only memory", either. It is true that modern operating systems have sophisticated implementations of virtual memory and address space, and provide for applications to specify different regions of virtual address space with different properties, such as read/write and read/only, and modern C++ compilers take full advantage of the operating system's resources, and will place objects that are designated as read-only in a separate memory segment, to achieve optimal virtual memory performance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.