Skip to content

Instantly share code, notes, and snippets.

@nelhage
Created November 27, 2017 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nelhage/995ddc683c48464c8a3b7b50063539d8 to your computer and use it in GitHub Desktop.
Save nelhage/995ddc683c48464c8a3b7b50063539d8 to your computer and use it in GitHub Desktop.
Which of these compile?
#include <memory>
using namespace std;
void accept_val(unique_ptr<int> i);
void accept_ref(unique_ptr<int> &i);
void accept_cref(const unique_ptr<int> &i);
void accept_rref(unique_ptr<int> &&i);
int main(int argc, char **argv) {
unique_ptr<int> i = make_unique<int>(1);
accept_val(i); // 1 (val-val)
accept_val(move(i)); // 2 (val-move)
accept_val(make_unique<int>(1)); // 3 (val-temp)
accept_ref(i); // 4 (ref-val)
accept_ref(move(i)); // 5 (ref-move)
accept_ref(make_unique<int>(1)); // 6 (ref-temp)
accept_cref(i); // 7 (cref-val)
accept_cref(move(i)); // 8 (cref-move)
accept_cref(make_unique<int>(1)); // 9 (cref-temp)
accept_rref(i); // 10 (rref-val)
accept_rref(move(i)); // 11 (rref-move)
accept_rref(make_unique<int>(1)); // 12 (rref-temp)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment