On Friday, 6 September 2019 at 09:49:33 UTC, Johan Engelen wrote:
On Friday, 6 September 2019 at 09:14:31 UTC, Andrew Edwards
wrote:
C++ allows the for following:
struct Demo
{
float a, b, c, d;
Demo() { a = b = c = d = 0.0f; }
Demo(float _a, float _b, float _c, float _d) {
a = _a;
b = _b;
c = _c;
d = _d;
}
float operator[] (size_t i) const { return (&a)[i]; } //[3]
"(&a)[i]" is undefined behavior in C++. You cannot index into
struct members like that. Of course it may work in certain
cases, but UB is UB. Don't do it!
I found a more detailed explanation for you:
https://stackoverflow.com/questions/40590216/is-it-legal-to-index-into-a-struct
-Johan
Thanks for the clarification and link. This is a example taken
from a popular library I’m trying to port to D. I’m not trying to
do it in C++ myself, just to understand how to interface to the
code so that I can get a reference example compiled in D.
Andrew