self reference and copy ability / movability
This file contains 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 <cstddef> | |
#include <cstdint> | |
#include <cstdio> | |
// ptr_to_buffer make this struct not a regular type, i.e. | |
// not trivially copyable/moveable | |
struct StructA { | |
char* const ptr_to_buffer = buffer; // dependency to representation | |
size_t const buffer_size = sizeof buffer; | |
char buffer[64]; | |
}; | |
static bool valid(StructA const& x) | |
{ | |
return x.ptr_to_buffer == x.buffer; | |
} | |
static void print(StructA const& x) | |
{ | |
std::printf("%s", | |
valid(x) ? "// is valid " | |
: "// is INVALID (representation moved) "); | |
std::printf("{\n"); | |
std::printf(" buffer = %zu;\n", intptr_t(x.buffer)); | |
std::printf(" buffer_size = %zu;\n", x.buffer_size); | |
std::printf(" ptr_to_buffer = %zu;\n", intptr_t(x.ptr_to_buffer)); | |
std::printf("}\n"); | |
} | |
int main(int, char **) | |
{ | |
StructA b = {}; | |
StructA a = b; // there ought to be maybe a warning here no? | |
printf("a: "); print(a); | |
printf("b: "); print(b); | |
auto c = StructA{}; | |
printf("c: "); print(c); | |
return 0; | |
}; | |
Author
nil-ableton
commented
Mar 29, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment