Reusing a float buffer for doubles without undefined behaviour

后端 未结 5 715
情书的邮戳
情书的邮戳 2021-02-05 04:19

In one particular C++ function, I happen to have a pointer to a big buffer of floats that I want to temporarily use to store half the number of doubles. Is there a method to use

5条回答
  •  庸人自扰
    2021-02-05 04:59

    This problem cannot be solved in portable C++.

    C++ is strict when it comes to pointer aliasing. Somewhat paradoxically this allows it to compile on very many platforms (for example where, perhaps double numbers are stored in different places to float numbers).

    Needless to say, if you are striving for portable code then you'll need to recode what you have. The second best thing is to be pragmatic, accept it will work on any desktop system I've come across; perhaps even static_assert on compiler name / architecture.

提交回复
热议问题