What does static_assert do, and what would you use it for?

前端 未结 8 728
生来不讨喜
生来不讨喜 2020-12-04 06:21

Could you give an example where static_assert(...) (\'C++11\') would solve the problem in hand elegantly?

I am familiar with run-time assert(...)<

8条回答
  •  旧巷少年郎
    2020-12-04 06:56

    One use of static_assert might be to ensure that a structure (that is an interface with the outside world, such as a network or file) is exactly the size that you expect. This would catch cases where somebody adds or modifies a member from the structure without realising the consequences. The static_assert would pick it up and alert the user.

提交回复
热议问题