Skip to content

Instantly share code, notes, and snippets.

@Adobe-Android
Created September 18, 2020 16:36
Show Gist options
  • Save Adobe-Android/8cfbd5d32c3f609337c772fb358ebf6c to your computer and use it in GitHub Desktop.
Save Adobe-Android/8cfbd5d32c3f609337c772fb358ebf6c to your computer and use it in GitHub Desktop.
Demonstrates copy semantics on member variables and C++17 structured binding.
#include <cstdio>
struct Point {
int x, y;
};
Point make_transpose(Point p) {
int tmp = p.x;
p.x = p.y;
p.y = tmp;
// structured binding in C++17.
return { p.x, p.y };
}
int main() {
// Demonstrates copy by value.
Point p{10, 4};
// Unpack results.
auto [p1, p2] = make_transpose(p);
printf("Original:\n");
printf("%d\n", p.x);
printf("%d\n\n", p.y);
printf("Modified:\n");
printf("%d\n", p1);
printf("%d\n", p2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment