Does the following snippet utilize undefined/unspecified/etc. behavior?
#include <cstddef>
#include <iostream>
#include <string>
class Test {
std::string s1{"s1"}, s2{"s2"};
std::ptrdiff_t offset = (char*)(&s2) - (char*)(this);
public:
std::string& get() { return *(std::string*)((char*)(this) + offset); }
};
int main() {
Test test;
std::cout << Test{test}.get(); // note the copy
}
The purpose of that offset is pointing at either s1 or s2 (chosen at runtime) and containing no special logic for copying/moving/accessing. std::string here is just an example of a non-trivial-anything class.