Could it be the case that sizeof(T*) != sizeof(const T*)?

前端 未结 4 909
遥遥无期
遥遥无期 2021-02-06 20:30

I\'m arguing with my boss about this. They say \"Yes they can be different.\"

Is it possible that sizeof(T*) != sizeof(const T*) for a type T?<

4条回答
  •  野性不改
    2021-02-06 20:57

    As far as standard C++ is concerned, they cannot differ. C and C++ are similar on this point.

    But there are many architectures out there with compilers written for them that do not follow this rule. Indeed then we are not really talking about standard C++, and some folk would argue then that the language is not C++, but my reading of your question (prior to the addition of the language lawyer tag) is more about the possibility.

    In which case, it is possible. You may well find that a pointer to ROM (therefore const) is a different size to a pointer to RAM (const or non const.)

    If you know for sure that your code will only wind up on a standards complaint compiler then your assumption is correct. If not, then I'd think carefully about relying on their sizes being the same.

提交回复
热议问题