Created
September 18, 2020 16:36
-
-
Save Adobe-Android/8cfbd5d32c3f609337c772fb358ebf6c to your computer and use it in GitHub Desktop.
Demonstrates copy semantics on member variables and C++17 structured binding.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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