c++ return value optimization and returning complex class problem

Viewed 41

according to This question & answer when we return an object from a function, it is compiler and c++ version dependent how it is returned, it could be moved, copied or elided.
imagine I have complex classes such as(could be even more complicated)


class Basket{
    public:
        Basket (); // ctor 
        ~Basket (); // dtor
        Basket (Basket &); // copy ctor
        Basket (Basket &&); // move ctor
        Basket & operator= (Basket &); // copy assignment
        Basket & operator= (Basket &&); // move assignment
        // getter setters
    private:
        vector<Products> product_list;
        vector<Liked> liked_list;
}

class Customer
{               // Note: All methods have side effects
  public: 
  
    Customer (); // ctor 
    ~ Customer (); // dtor
    Customer (Customer &); // copy ctor
    Customer (Customer &&); // move ctor
    Customer & operator= (Customer &); // copy assignment
    Customer & operator= (Customer &&); // move assignment
    // getter setters
    
  private:
    vector<int*> some_ptrs;
    vector<Adress> adress_list;
    CustomerInfo customer_info;
    vector<Basket> basket;
};

Customer get_customer(int id) {
    Customer customer = db.get_customer(id);
    return customer;
}

in cases where my copy and move semantics have side effects and it's not clear which method is used, how can I write a code that's consistent and efficient?
What are the best practices to ensure that my code works as expected and efficient regardless of the implementation and the version of the c++ that is used.

0 Answers
Related