简体   繁体   English

请帮助我理解这种语法(在C ++中实现静态断言)

[英]Please help me understand this syntax (implementing static assert in C++)

This syntax was used as a part of an answer to this question : 此语法用作此问题答案的一部分:

template <bool>
struct static_assert;

template <>
struct static_assert<true> {}; // only true is defined

#define STATIC_ASSERT(x) static_assert<(x)>()

I do not understand that syntax. 我不明白那种语法。 How does it work? 它是如何工作的?

Suppose I do 假设我这样做

STATIC_ASSERT(true);

it gets converted to 它被转换为

static_assert<true>();

Now what? 怎么办?

STATIC_ASSERT(true);

indeed means 确实意味着

static_assert<true>();

which evaluates to nothing. 评估为空。 static_assert<true> is just an empty structure without any members. static_assert<true>只是一个没有任何成员的空结构。 static_assert<true>() creates an object of that structure and does not store it anywhere. static_assert<true>()创建该结构的对象,不会将其存储在任何位置。

This simply compiles and does nothing. 这简单地编译并且什么都不做。

On the other hand 另一方面

STATIC_ASSERT(false);

means 手段

static_assert<false>();

which results in compilation error. 这导致编译错误。 static_assert has no specialization for false . static_assert没有false So a general form is used. 所以使用一般形式。 But the general form is given as follows: 但一般形式如下:

template <bool>
struct static_assert;

which is just a declaration of a structure and not its definition. 这只是一个结构的声明,而不是它的定义。 So static_assert<false>() causes compilation error as it tries to make an object of a structure which is not defined. 因此, static_assert<false>()会在尝试创建未定义的结构的对象时导致编译错误。

static_assert<true>(); makes that 这样做

template <>
struct static_assert<true> {}

templated struct specialization temporary object creation being done - a call to constructor and later to a destructor that both will be hopefully eliminated by the optimizer since they do nothing. 模板化结构专门化临时对象创建正在完成 - 对构造函数的调用以及稍后对析构函数的调用,两者都有望被优化器消除,因为它们什么都不做。 Since there's only a specialization for true and no generic version of the template struct all constructs that evaluate to static_assert<false>(); 由于只有true的特殊化而且没有通用版本的模板结构,所有构造都计算为static_assert<false>(); will just not compile. 将不会编译。

In the expression 在表达中

static_assert<true>();

since static_assert<true> is a type, it will call the constructor of static_assert<true> . 因为static_assert<true>是一个类型,所以它将调用static_assert<true>的构造函数。 As static_assert<true> is specialized to an empty struct, nothing will be affected. 由于static_assert<true>专用于空结构,因此不会影响任何内容。


However, in 但是,在

static_assert<false>();

as there is no specialization for static_assert<false> , the generic definition 因为static_assert<false>没有专门化,所以通用定义

template <bool>
struct static_assert;

will be used. 将会被使用。 But here, the type static_assert<B> is incomplete . 但是在这里,类型static_assert<B>不完整的 So calling constructor of static_assert<B> will result in compilation error. 因此调用static_assert<B>构造函数会导致编译错误。


Therefore, this is called "static assert" as the statement will abort the compilation if the expression evaluates to false , similar to the normal assert() function that will kill the program in runtime. 因此,这称为“静态断言”,因为如果表达式求值为false ,语句将中止编译,类似于将在运行时终止程序的正常assert()函数

Well, I guess it is about template specialization. 好吧,我想这是关于模板专业化的。 STATIC_ASSERT(true) will compile successfully, because there is a definition (not just a declaration) of "static_assert< true >". STATIC_ASSERT(true)将成功编译,因为存在“static_assert <true>”的定义(不仅仅是声明)。

STATIC_ASSERT(false) will be rejected by the compiler, because there is only a declaration for "static_assert< false >" and no definition. STATIC_ASSERT(false)将被编译器拒绝,因为只有“static_assert <false>”的声明而且没有定义。

Update: for visual studio, STATIC_ASSERT(true) is ok, but STATIC_ASSERT(false) triggers the error: "error C2514: 'static_assert<__formal>' : class has no constructors [ with __formal = false ]" 更新:对于visual studio,STATIC_ASSERT(true)没问题,但是STATIC_ASSERT(false)触发错误:“错误C2514:'static_assert <__ formal>':类没有构造函数[with __formal = false]”

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

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