静态断言失败
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their way to become runtime problems are harder to detect and may go unchecked until such time that the code reaches a customer. The later the defect is identified the more costly it is in terms of time and money.
错误会发生。 对于程序员而言,这是事实。 检测错误的方式和时间对产品的质量和成本有很大的影响。 如果可能且可行的话,最好在编译时检测错误。 导致错误成为运行时问题的错误更难检测,并且可能直到代码到达客户手中时才被检查。 越早发现缺陷,就时间和金钱而言,损失就越大。
A static assertion is similar to a runtime assertion, in so far as it allows the programmer to assert that an expression must be true, or an error message must be raised. The difference is that static assertions are triggered at compile time rather than runtime. How is this useful? Well, how about a situation where we've had to make assumptions about the capacity of a specific type, say int?
静态断言类似于运行时断言,只要它允许程序员断言表达式必须为真,否则必须引发错误消息。 区别在于静态断言是在编译时而不是在运行时触发的。 这有什么用? 好吧,在我们不得不对特定类型的容量进行假设的情况下,例如int呢?
The C++ Standard makes no specific claims about how big an int will be other than it "has the natural size suggested by the architecture of the execution environment."
除了“具有执行环境的体系结构所建议的自然大小”之外,C ++标准对int的大小没有特别声明。
What does this actually mean? Well, in reality, it means the size of an int could be any size. The same is true of other types; a long must be as least as big as an int and an int at least s big as a short. Clearly, making an assumption about type size is non-portable and potentially dangerous, and should only be done in controlled cases, such as writing an OS kernel for a particular processor with a particular compiler. As soon as the programmer loses the ability to specify the compiler and the target architecture, type size assumptions go out the window. So, how does a static assertion help? Good question, let's see...
这到底是什么意思? 好吧,实际上,这意味着int的大小可以是任何大小。 其他类型也是如此。 long必须至少与int一样大,而int至少应与short一样大。 显然,关于类型大小的假设是不可移植的,并且有潜在的危险,并且仅应在受控情况下进行,例如使用特定的编译器为特定的处理器编写OS内核。 一旦程序员失去了指定编译器和目标体系结构的能力,类型大小的假设就会浮出水面。 那么,静态断言有何帮助? 好问题,让我们看看...
Wouldn't it be nice if we could assert our assumption about the size of a type at compile time so that if this assumption breaks, we are alerted immediately? For example, the code is built by a different compiler than the author used on a platform he did not anticipate. Enter static assertions. The trick is to turn to templates and, specifically, make use of the fact that a template variant, that is not instantiated, will not cause a compile time failure even if it contains erroneous code.
如果我们可以在编译时断言关于类型大小的假设,这样如果该假设中断了,我们会立即收到警报,这会很好吗? 例如,该代码是由与他在未曾预料到的平台上使用的作者不同的编译器构建的。 输入静态断言。 诀窍是求助于模板,特别是要利用这样的事实,即未实例化的模板变体即使包含错误代码也不会导致编译时失败。
Create a template function that takes a bool template value parameter (not a function parameter). Within the function create a char array and use the value of the bool to determine the size. In the case where the bool template value is true, a char array of one element will be created. Since this will never be used the compiler should happily optimize this away. In the case where the bool template value is false, the compiler will try to generate a template function that creates a char array of zero elements. Since this is invalid C++, a compiler error will ensue. We can now use a simple macro to facilitate the use of this template and then use a sizeof() to assert the sizes of the types we are assuming. Of course, any compile time constant can be asserted with a static assert, this is just one example of usage.
创建一个采用bool模板值参数(而不是函数参数)的模板函数。 在函数内创建一个char数组,并使用bool的值确定大小。 如果bool模板值为true,则将创建一个元素的char数组。 由于将永远不会使用它,因此编译器应该愉快地对其进行优化。 在bool模板值为false的情况下,编译器将尝试生成一个模板函数,该函数创建一个零元素的char数组。 由于这是无效的C ++,将发生编译器错误。 现在,我们可以使用一个简单的宏来简化此模板的使用,然后使用sizeof()声明所假设类型的大小。 当然,任何编译时间常数都可以使用静态断言来断言,这只是用法的一个示例。
template <bool B>
inline void STATIC_ASSERT_IMPL()
{
// B will be true or false, which will implictly convert to 1 or 0
char STATIC_ASSERT_FAILURE[B] = {0};
}
#define STATIC_ASSERT(B) STATIC_ASSERT_IMPL <B>()
int main()
{
// On a Windows 32 bit platform with Visual Studio 2005 this will not fail
STATIC_ASSERT(sizeof(int) == 4);
// On a Windows 32 bit platform with Visual Studio 2005 this *will* fail
STATIC_ASSERT(sizeof(int) == 3);
}
翻译自: https://www.experts-exchange.com/articles/225/Static-assertions-in-C.html
静态断言失败