Created attachment 35659 [details] Archive contains wrap-int-[012].cxx and makefile Ideally, replacing the type "int" with a wrapper class that contains a single "int" value as a member should not impose any performance penalty. Unfortunately, that is not the case if the wrapper class has a non-default constructor, which it almost certainly will. The attached archive contains three programs, which can be compiled to assembly language using the included makefile. wrap-int-0.cxx contains a very simple integer computation. wrap-int-1.cxx does the same thing, but the "int" type has been replaced by a wrapper class. The generated assembly instructions are same, so there is no performance penalty for using the wrapper class. wrap-int-2.cxx is the same as wrap-int-1.cxx except that a constructor has been added to the wrapper class. One would expect a good optimizer to generate the same code for this program as for the other two programs, but in fact the generated code is a good deal larger. I'm guessing that what is happening is that if a type has a non-default constructor, the C++ front end treats constants of that type as variables rather than as contstants, so that even simple optimizations like constant folding are no longer performed by that back end.
If you make the constructors and operator+ constexpr and compile with -std=gnu++14 then you remove the overhead.
PR 65197 has links to a number of related PRs. The middle-end has no code to handle the case where we detect "late" (i.e. not in the front-end) that the initialization is constant.
Thanks to the pointer provided by Marc Glisse, I found that this issue was reported back in 2001, in PR 4131. *** This bug has been marked as a duplicate of bug 4131 ***