How portable is using the low bit of a pointer as a flag?

前端 未结 4 967
抹茶落季
抹茶落季 2020-12-28 17:09

If there is for example a class that requires a pointer and a bool. For simplicity an int pointer will be used in examples, but the pointer type is

4条回答
  •  失恋的感觉
    2020-12-28 17:16

    It's very portable, and furthermore, you can assert when you accept the raw pointer to make sure it meets the alignment requirement. This will insure against the unfathomable future compiler that somehow messes you up.

    Only reasons not to do it are the readability cost and general maintenance associated with "hacky" stuff like that. I'd shy away from it unless there's a clear gain to be made. But it is sometimes totally worth it.

提交回复
热议问题