Constexpr for creating objects

I am trying to find out if there is an increase in performance for creating objects with constexpr instead of the usual one.

Here is the code snippet for constexpr .

 class Rect { const int a; const float b; public: constexpr Rect(const int a,const float b) : a(a),b(b){} }; int main() { constexpr Rect rect = Rect(1,2.0f); } 

And without constexpr .

 class Rect { int a; float b; public: Rect(int a, float b) : a(a),b(b){} }; int main() { Rect rect = Rect(1,2.0f); } 

I expected that constexpr would be much less code for constexpr , since the memory had to be initialized at compile time.

Am I using constexpr correctly? And if that is not the case, can you use constexpr to create objects at compile time and then use them at no cost to the runtime?

Thanks!

+5
source share
1 answer

It turned out that I included some headers that were responsible for the similarity of the code.

The answer is that there is a big difference between the two cases.

When compiling without optimization , there is a significant difference in the generated code ... -cdhowie

0
source

Source: https://habr.com/ru/post/1269068/


All Articles