繁体   English   中英

如何将 static_assert 与 sizeof 和 stringify 结合起来?

[英]How to combine static_assert with sizeof and stringify?

内存使用在我的应用程序中非常关键。 因此,我有特定的断言,可以在编译时检查内存大小,如果大小与我们之前认为正确的大小不同,则给出 static_assert。

我定义了一个这样的宏:

#define CHECKMEM(mytype, size) static_assert((sizeof(objectType) == size)), "Size incorrect for " #mytype "!");

这个宏使得写这个变得非常容易:

CHECKMEM(Book,144);
CHECKMEM(Library,80);

问题是,当这个 static_assert 关闭时,可能很难找出新的大小应该是多少(例如,通过使用隐藏的编译器选项“/d1 reportAllClassLayout”)。 如果我可以包括实际尺寸会更方便,所以不是:

书的尺寸不正确!

它会显示

书的尺寸不正确,(预期 144,尺寸为 152)

我试着写这样的东西:

#define CHECKMEM(mytype, size) static_assert((sizeof(objectType) == size)), "Size incorrect for " #mytype "! (expected" #size ", size is " #sizeof(mytype) ")");

但是您不能在函数调用中使用字符串化 (#) 运算符。

我还尝试添加 double-stringize 技巧,如下所示:

#define STR1(x) #x 
#define STR2(x) STR1(x) 
#define CHECKMEM(mytype, size) static_assert((sizeof(objectType) == size)), "Size incorrect for " #mytype "! (expected" #size ", size is " STR2(sizeof(mytype)) ")");

但不是打印size is 152 ,而是打印size is sizeof(Book)

有没有办法在 static_assert 中将 sizeof 的结果字符串化?

我会在函数模板上使用调度来进行检查:

#include <cstddef>

template <typename ToCheck, std::size_t ExpectedSize, std::size_t RealSize = sizeof(ToCheck)>
void check_size() {
  static_assert(ExpectedSize == RealSize, "Size is off!");
}

struct foo
{
  char bla[16];
};

int main()
{
  check_size<foo, 8>();
  return 0;
}

结果是:

In instantiation of ‘void check_size() [with ToCheck = foo; long unsigned int ExpectedSize = 8ul; long unsigned int RealSize = 16ul]’:
bla.cpp:15:22:   required from here
bla.cpp:5:1: error: static assertion failed: Size is off!

调试信息在回溯的模板参数中。

如果这真的更好,您将不得不做出决定,这也取决于编译器。 它还使您能够使用模板映射隐藏预期大小,以总结最大大小和其他奇特的东西。

根据您的编译器,模板可能有助于:

template<int s, int t> struct check_size {
  static_assert(s == t, "wrong size");
};
check_size<2+2, 5> doubleplusungood;

海合会输出:

prog.cpp: In instantiation of 'check_size<4, 5>':
prog.cpp:5:20:   instantiated from here
prog.cpp:2:3: error: static assertion failed: "wrong size"

正如您发现的那样,问题就在这里(另请参阅这个非常相似的问题):

#define CHECKMEM(mytype, size)  #sizeof(mytype)

这是不可能的,因为字符串化是由预处理器完成的,而 sizeof 是在编译期间计算的。

如果您可以稍微修改结构的定义并且不介意一些丑陋,那么这是一个仅标头的替代解决方案。

template <int RealSize = 0, int ExpectedSize = 0>
struct _MyStruct {
    static_assert(RealSize == ExpectedSize, "size is invalid");

    int x;
};

typedef _MyStruct<sizeof(_MyStruct<>), 4> MyStruct;

铛输出:

prog.cpp:4:5: error: static_assert failed "size is invalid"
    static_assert(RealSize == ExpectedSize, "size is invalid");
    ^             ~~~~~~~~~~~~~~~~~~~~~~~~
prog.cpp:12:14: note: in instantiation of template class '_MyStruct<4, 8>' requested here
    MyStruct m;

这里需要注意的是,只有在某处实例化类型时才会进行检查——仅使用指针不会触发错误,因此绝对不是适合所有情况!

一个简单实用的解决方案是使用 2 个static_assert -s:

#define CHECKMEM(mytype, size) \
static_assert( sizeof(mytype) <= (size), "Size of " #mytype " is greater than " #size "!" ); \
static_assert( sizeof(mytype) >= (size), "Size of " #mytype " is less than "    #size "!" )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM