0
#include <iostream>

struct A
{
    int arr[666];
};

int main()
{
    std::cout<<sizeof( (static_cast<A*>(nullptr))->arr); //OK?
}

The code above could be run without error. Does standard say anything about such situation?

choxsword
  • 3,187
  • 18
  • 44
  • @πάντα - This is in an unevaluated operand. None of the answers in the dupe address that. – StoryTeller - Unslander Monica May 05 '19 at 12:27
  • 1
    @StoryTeller Added another link with standard cites. Feel free to add better ones or reopen the question. – πάντα ῥεῖ May 05 '19 at 12:28
  • @πάνταῥεῖ could you please provide the previous linkage in the comment? I want to see that as well. – choxsword May 05 '19 at 12:39
  • @πάνταῥεῖ But I could just see one link indeed. – choxsword May 05 '19 at 12:42
  • @bigxiao Oops, someone must have removed the original one. Wasn't me. – πάντα ῥεῖ May 05 '19 at 12:43
  • @πάνταῥεῖ Well, I would aprreciate it if you could provide that link again in the comment. I could not find that question. – choxsword May 05 '19 at 12:45
  • @bigxiao It was this one: https://stackoverflow.com/questions/6793262/why-dereferencing-a-null-pointer-is-undefined-behaviour – πάντα ῥεῖ May 05 '19 at 13:10
  • From what I can see, the other thread didn't have to do with member access, so there might be a usefully different answer here. Since C++17, apparently you should be doing this: `struct A { int arr[666]; }; std::cout << sizeof(A().arr);`. See https://en.cppreference.com/w/cpp/language/implicit_conversion#Temporary_materialization. There is also the C way, which has been around a bit longer and, from what it looks like on cppreference, does the same thing anyway (declare a temporary object, then elide it as unused): `std::cout << sizeof( ((struct S) { }).a )` – l.k May 05 '19 at 13:27

0 Answers0