Is it possible to provide copying?

Copying elision is a neat optimization technique and, in some cases, relying on copy elision can actually be faster than passing links “manually”.

So, suppose you define a critical code path in which you rely on the equalization of the copy being done by your compiler for the code path for maximum performance.

But now you are relying on compiler optimization.

Is there any (compiler-specific, obviously) way to make sure that the copy ellipse is actually executing and that the compiler (or other tool) generates a warning / error if the copy exception cannot be executed?

(I am thinking of something remotely similar to Visual C ++ __forceinline , which generates a warning if a function marked this way is not compiled by the compiler.)

+7
source share
3 answers

Not really, except for placing assert(false); in the copy constructor.

Otherwise, use your favorite profiler to measure that the interesting parts of the application are fast enough.

+4
source

Not.

But you can write the equivalent, albeit completely unreadable, code:

 BigObj f() { BigObj x(g()); x.someMethod(); return x; } //... BigObj z = f(); //... 

translates (with a copy of elision) to:

 void f(BigObj* obj) { new(obj) BigObj(g()); obj->someMethod(); } //... char z[sizeof(BigObj)]; f((BigObj*)&z[0]); //... ((BigObj*)&z[0])->~BigObj(); 

But seriously, just write your code so that the compiler can strip the copy. That is, it returns only one object without branching:

 BigObj f() { BigObj x, y; // use x and y if(condition) return x; else return y; // cannot be elided } BigObj f() { if(condition) { BigObj x; return x; } else { BigObj y; return y; } // can be elided } 
+4
source

In C ++ 1z (expected 2017), in some cases, you will need to guarantee copy permission:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0135r0.html

According to cppreference.com compiler utility support, the GCC 7+ and Clang 4+ wikis provide this.

Fortunately, the optimization side should not require the inclusion of support for new languages, since this is pure optimization (in accordance with the old language standards).

Also, in order for the copy constructor to be unavailable when optimization is applied, it will probably require the inclusion of a newer language standard at compile time or the use of free or advanced mode, which does not require strict compliance (for example, potentially GCC -fpermissive ).

+1
source

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


All Articles