Vlad Didenko
Vlad Didenko

Reputation: 4791

Deleted copy and assignment constructors in request_handler of the http server example

Why are the copy and assignment constructors deleted in request_handler in the ASIO http server example? Here is the header to save the lookup:

class request_handler
{
public:
  request_handler(const request_handler&) = delete;
  request_handler& operator=(const request_handler&) = delete;

  /// Construct with a directory containing files to be served.
  explicit request_handler(const std::string& doc_root);

  /// Handle a request and produce a reply.
  void handle_request(const request& req, reply& rep);

private:
  /// The directory containing the files to be served.
  std::string doc_root_;

  /// Perform URL-decoding on a string. Returns false if the encoding was
  /// invalid.
  static bool url_decode(const std::string& in, std::string& out);
};

If anything, seems that the only field doc_root_ can be made const and default constructors would do if needed? The code does not actually copy the handler in the published example. However, I am exploring the possibility in my code and it would be great to understand if I am missing something very basic.

Upvotes: 0

Views: 43

Answers (1)

Arunmu
Arunmu

Reputation: 6901

It makes sense since copying doc_root_ could result in extra memory allocation. That means for each new connection, it would copy the request_handler to the connection thereby doing a memory allocation (if SSO is not applicable on the string size ofcourse).

If I can avoid unnecessary allocations, I would definitely do that, more so in case where request_handler is not storing any per connection specific data.

NOTE: I am ready to move this as a comment if it doesn't answer the question entirely.

Upvotes: 1

Related Questions