Compile-time check to make sure that there is no padding anywhere in a struct

后端 未结 4 1885
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 21:58

Is there a way to write a compile-time assertion that checks if some type has any padding in it?

For example:

struct This_Should_Succeed
{
    int a;         


        
4条回答
  •  孤街浪徒
    2020-12-11 22:21

    Since C++17 you might be able to use std::has_unique_object_representations.

    #include 
    
    static_assert(std::has_unique_object_representations_v); // succeeds
    static_assert(std::has_unique_object_representations_v); // fails
    

    Although, this might not do exactly what you want it to do. Check the linked cppreference page for details.

提交回复
热议问题