问如何把下面的assert 换掉,让在编译的时候,编译器提示错误。
void Convert( int source, char value[4])
{
assert( sizeof(int) == sizeof( value);
*(int*)value = source;
}
Foo<sizeof(int)-sizeof(value)> bar;
Xihe Yu wrote:
> static_assert
>
> Best Regards!
>
> -
> KrzyCube | 方块
>
>
> 2009/8/27 Roc Zhang <zhyg...@gmail.com <mailto:zhyg...@gmail.com>>
dave@gentoo-on-x200s ~/devel/test $ g++ test.cc
test.cc: In function ‘int main(int, char**)’:
test.cc:5: error: size of array ‘size_should_be_the_same’ is negative
但为什么我觉得楼主的问题在责任在assert(), assert()本身应该做到这一点.
如果assert()做不到, 应该改一下assert() 的实现, 而不是换掉那一行
#define assert(x) \
{\
if (compiletime_const(x))\
static_assert(x);\
else\
runtime_assert(x);\
} while (0)
PS: 我只知道怎样在gcc实现compile_time_const(), 其他编译器完全不知道...
大体上分两类: 一类是利用C++ 里面编译常量: 比如数组下表不能为0, 枚举为整形;
第二类: 利用模板特化,编译自己根据条件展开。
对于 Loki::CompileTimeError 实现,不知道,看看再说。
受教了,谢谢大家,用了C++五年惭愧啊。看样子还得好好看几本书。
大体上分两类: 一类是利用C++ 里面编译常量: 比如数组下表不能为0, 枚举为整形;
第二类: 利用模板特化,编译自己根据条件展开。
对于 Loki::CompileTimeError 实现,不知道,看看再说。
On Aug 27, 6:04 pm, Roc Zhang <zhyg6...@gmail.com> wrote:
使用预处理器定义断言,是不是可以在编译阶段处理断言