16 Ekim 2016 Pazar

Copy Elision

Giriş
Copy Elision notlarım aşağıda. Copy Elision'ın özel bir hali de Return Value Optimization

Copy Elision ve Yan Etkileri
Copy Elision yan etkilere sahiptir, mesela copy constructor, destructor gibi metodların çağrılmamasını sağlar.

Copy Elision Yapılmayabilir - C++17'den önce
Copy Elision bazı durumlarda derleyici tarafından dikkate alınmayabilir.
Copy elision is the only allowed form of optimization that can change the observable side-effects. Because some compilers do not perform copy elision in every situation where it is allowed (e.g., in debug mode), programs that rely on the side-effects of copy/move constructors and destructors are not portable.
Copy Elision - C++17
Açıklaması şöyle
Under the following circumstances, the compilers are required to omit the copy- and move- constructors of class objects even if copy/move constructor and the destructor have observable side-effects:
  • In initialization, if the initializer expression is a prvalue and the cv-unqualified version of the source type is the same class as the class of the destination, the initializer expression is used to initialize the destination object:
    T x = T(T(T())); // only one call to default constructor of T, to initialize x
Copy Constructor
Elimizde şöyle bir sınıf olsun
Server::Server(int port) {
    // initialize some class variables
    port_ = port;
    //...
}
Copy Elision sayesinde Copy Constructor'ın çağrılmasına gerek kalmadan şöyle yaparız.
int port = 3000;
Server server = Server(port);
Copy Elision ve Copy/Move Constructor - C++17'den önce
Burada dikkat edilmesi gereken nokta şu. Copy constructor veya Move Constructor çağrılmasa bile erişilebilir olmalıdır.
.... The last step is usually optimized out and the result of the conversion is constructed directly in the memory allocated for the target object, but the appropriate constructor (move or copy) is required to be accessible even though it's not used.


Hiç yorum yok:

Yorum Gönder