Skip to content

Instantly share code, notes, and snippets.

@ajvengo
Created April 7, 2017 19:49
Show Gist options
  • Save ajvengo/a7c7cc6f9a8ee0a6fffa19b2d07b53c4 to your computer and use it in GitHub Desktop.
Save ajvengo/a7c7cc6f9a8ee0a6fffa19b2d07b53c4 to your computer and use it in GitHub Desktop.
Test copyable vs movable arguments for overloaded functions
#include <type_traits>
#include <iostream>
using namespace std;
// must be used for movable arguments only
void f(int&& x) {
using X = decltype(x);
cout << __PRETTY_FUNCTION__ << ' ' << std::is_rvalue_reference<X>::value << endl;
}
// must be used for copyable arguments only
void f(const int& x) {
using X = decltype(x);
cout << __PRETTY_FUNCTION__ << ' ' << std::is_rvalue_reference<X>::value << endl;
}
int main() {
int i = 2;
int& iref = i;
const int& const_iref = i;
f(4);
f(4 + 2);
f(i);
f(std::move(i));
f(iref);
f(std::move(iref));
f(const_iref);
f(std::move(const_iref));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment