Skip to content

Instantly share code, notes, and snippets.

@acmorrow
Created February 5, 2013 19:57
Show Gist options
  • Save acmorrow/4717129 to your computer and use it in GitHub Desktop.
Save acmorrow/4717129 to your computer and use it in GitHub Desktop.
// C++11 offers a built-in "owning" pointer which allows transfer of ownership
// and manages lifetime but disallows copying. There is no runtime overhead for its use:
std::unique_ptr<socket> connect_to(const std::string& host, int port) {
/// ...
return new socket(fd);
}
{
auto connected = connect_to("www.10gen.com", 80);
connected->send("GET HTTP/1.1 index.html");
} // socket is automatically destroyed here.
auto connected = connect_to("www.mongodb.org", 80);
auto copy_of_connected = connected; // NOPE, won't compile, would alias
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment