简体   繁体   English

MSVC SAL与C ++ 2a合同

[英]MSVC SAL vs. C++2a Contract

在MSVC中,有SAL功能 ,可用于描述参数,结果等,而且效果很好,便携性也不错,C ++ 2a契约功能似乎做同样的事情,有人可以给一些他们之间的差异?

There is no real overlap between SAL and contracts. SAL和合同之间没有真正的重叠。 SAL is about annotating parameters and return values for describing some aspects of how the function uses them. SAL是关于注释参数和返回值,用于描述函数如何使用它们的某些方面。 Contracts about about describing what the function requires of its parameters and what the caller can expect of the return value. 关于描述函数对其参数的要求以及调用者对返回值的期望的约定。 These sound similar, but they are very different. 这些听起来很相似,但它们却截然不同。

For example, SAL has notions of whether a parameter is an input, output, or in/out parameter. 例如,SAL具有参数是输入,输出还是输入/输出参数的概念。 That is a matter of what the function does with the parameters. 这是函数对参数的作用。 Contracts have no such notion because they don't care what the function does with the parameter. 合同没有这样的概念,因为它们不关心函数对参数的作用。

Now there is some overlap. 现在有一些重叠。 Sometimes, what a function expects and what a function does align. 有时,函数期望什么以及函数对齐的是什么。 For example, if a function expects a pointer parameter to not be nullptr , you would apply an [[pre: param != nullptr]] contract to that function. 例如,如果函数期望指针参数不是nullptr ,则应将[[pre: param != nullptr]]约定应用于该函数。 However, the SAL notion of _In_ pointer parameter annotations covers the same general idea: if the function uses the parameter as a valid pointer to an object, then naturally it cannot be nullptr . 但是, _In_指针参数注释的SAL概念涵盖了相同的一般概念:如果函数使用参数作为对象的有效指针,那么它自然不能是nullptr So _In_ protects from that. 所以_In_可以保护它。

SAL is ultimately about describing how the function uses the data; SAL最终将描述函数如何使用数据; contracts are about the function's interface with the calling code. 契约是关于函数与调用代码的接口

Also, SAL is extremely limited in what you can express. 此外,SAL的表达极为有限。 It has a few complex statements like "is a null-terminated string", but it has no foundation on which you can build your own. 它有一些复杂的语句,比如“是一个以空字符结尾的字符串”,但它没有基础可以构建自己的字符串。 Contracts are C++ expressions, and they therefore can verify anything that a C++ expression can verify. 契约是C ++表达式,因此它们可以验证C ++表达式可以验证的任何内容

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

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