12

According to the standard — an aggregate or union type that includes one of the aforementioned types among its elements or nonstatic data members (including, recursively, an element or non-static data member of a subaggregate or contained union), this is allowed:

struct foo {
  float x;
};

void bar(foo*);

float values[9];
bar(reinterpret_cast<foo*>(&values));

However, I am not sure whether the following example also honors this rule:

struct foo {
  float x;
  float y;
  float z;
};

void bar(foo*);

float values[9];
assert((sizeof(values) / sizeof(float)) % 3 == 0);  // sanity check
bar(reinterpret_cast<foo*>(&values));
andrean
  • 6,717
  • 2
  • 36
  • 43
  • 5
    What are you quoting that makes the first code block legal? AFAIK it is as illegal as the second code block. There could be padding at the end of the struct that throws off the alignment. – NathanOliver Jul 03 '19 at 20:46
  • I based the first example on the explanation from this answer: https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule/51228315#51228315 , although now that you mention, perhaps I misunderstood even that. – andrean Jul 03 '19 at 20:51
  • hmm. I'm not sure what it is trying to convey there. – NathanOliver Jul 03 '19 at 20:58
  • I believe the example from the linked answer attempts to prove that the compiler cannot assume that the two function parameters (`foo&`, `int&`) are not aliasing each other, that is, `foo` is a supposedly legal alias to an `int`? – andrean Jul 03 '19 at 21:05
  • @andrean I don't see which part of the linked answer you would have based this code on. Can you point out which passage you believe is relevant to this question? – François Andrieux Jul 03 '19 at 21:06
  • Before doing `reinterpret_cast(&values)` for `float values[9];`, read https://timsong-cpp.github.io/cppwp/n4659/basic.compound#def:pointer-interconvertible – Language Lawyer Jul 03 '19 at 21:08
  • My example is not actually there, I derived it, based on what I thought was proved in that answer (under the explanation of paragraph 11.6) – andrean Jul 03 '19 at 21:08
  • I wonder what happened to this sentence in the current draft (it's missing): http://eel.is/c++draft/basic.lval#11 – geza Jul 03 '19 at 21:29
  • @geza see http://wg21.link/p1359#2051 – Language Lawyer Jul 03 '19 at 21:31
  • 4
    Yeah, OP, the rule you are referring to was never applicable in C++, see http://wg21.link/p1359#2051 – Language Lawyer Jul 03 '19 at 21:34
  • @LanguageLawyer: thanks. This issue 2051 made me to ask this: https://stackoverflow.com/questions/56878519/what-happened-to-the-aggregate-or-union-type-that-includes-one-of-the-aforement – geza Jul 03 '19 at 22:13
  • Thanks everyone, I guess that does clear things up, neither options are legal C++ – andrean Jul 04 '19 at 10:41
  • 1
    @LanguageLawyer It took the enlightened and expert ppl in the committee how many years to notice that one of the most well known, most quoted, most discussed rule had inapplicable clauses? And they used to tell ppl that they understood what the rules meant. Tells a lot of the clarity and common understanding of C++. – curiousguy Jul 05 '19 at 02:01
  • 1
    @curiousguy: The members of the Committee did not think it should matter if a general category of actions that was classed as Undefined Behavior included some which should obviously be processed in ways described elsewhere in the Standard, because the Standard is not intended to forbid capriciously-defined implementations from behaving in needlessly-useless fashion. If parts of the Standard or an implementation's documentation describe the behavior of some action but another part says it's UB, the action need not be processed usefully on implementation whose customers don't need it to be,... – supercat Jul 08 '19 at 15:42
  • 1
    ...but should be processed usefully by quality implementations whose customers do need it to be, regardless of whether the Standard mandates such behavior or not. – supercat Jul 08 '19 at 15:43
  • Undefined behavior is undefined behavior, not implementation-defined behavior. The sanity check doesn't change that. – L. F. Sep 08 '19 at 08:14

0 Answers0