在什么情况下我们需要包括?

时间:2021-01-28 15:06:20

In what cases should we include cassert?

在什么情况下我们应该包括cassert?

4 个解决方案

#1


40  

In short, don't use it; use <assert.h>.

简而言之,不要使用它;使用< assert.h >。

C++11 removed any formal guarantee of a "c...." header not polluting the global namespace.

c++ 11删除任何正式的保证一个“C ....”头不污染全局命名空间。

It was never an in-practice guarantee, and now it's not even a formal guarantee.

这从来都不是实际的担保,现在甚至也不是正式的担保。

Hence, with C++11 there is no longer any conceivable advantage in using the "c...." header variants, while there is the distinct and clear disadvantage that code that works well with one compiler and version of that compiler, may fail to compile with another compiler or version, due to e.g. name collisions or different overload selection in the global namespace.

因此,与c++ 11不再有任何可能的优势在使用“C ....”头变异,虽然是截然不同的和明确的缺点,运行良好的代码编译器和编译器的版本,可能无法与另一个编译器或编译版本,由于如名称冲突或不同过载选择全局命名空间。

SO, while cassert was pretty meaningless in C++03 (you can't put a macro in a namespace), it is totally meaningless -- even as a special case of a general scheme -- in C++11.

因此,虽然cassert在c++ 03中毫无意义(您不能将宏放在名称空间中),但在c++ 11中,它是完全没有意义的——即使作为通用方案的一个特例。


Addendum, Dec 22 2013:

附录,2013年12月22日:

The standard defines each C++ C header <X.h> header in terms of the <cX> header, which in turn is defined in terms of the corresponding C library header.

标准定义了每个c++ C头 标头以 标头的形式表示,而 标头又以相应的C库标头定义。 。h>

C++11 §D.5/2:

c++ 11§D.5/2:

“Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope.”

“每个C标头,每个标头都有表单名的名称。h的行为就好像每个由相应的cname头放在标准库名称空间中的名称都放在全局名称空间范围内一样。

C++11 §D.5/3 (non-normative example):

c++ 11§D。5/3(非规范化的例子):

“The header <cstdlib> assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std.”

header 肯定地在名称空间std中提供了它的声明和定义。标题< stdlib。h>无疑地在全局命名空间中提供了相同的声明和定义,就像在C标准中一样。它还可以在名称空间std中提供这些名称。

Stack Overflow user C.R.’s comment made me aware that some versions of g++, such as MinGW g++ 4.7.2, are quite non-standard with respect to the <X.h> headers, lacking the overloads of e.g. sin that the C++ standard requires:

Stack Overflow用户C.R.他的评论让我意识到,g+的一些版本,比如MinGW g+ 4.7.2,对于 页眉,缺少c++标准要求的sin等重载: 来说是非常不标准的。h>

I already knew that MinGW g++ 4.7.2 also entirely lacks functions such as swprintf, and that it has ditto shortcomings in the pure C++ library such as lacking C++11 std::to_string. However, the information about it lacking the C function overloads was new to me.

我已经知道MinGW g+ 4.7.2也完全没有swprintf这样的函数,并且在纯c++库中也有同样的缺点,比如缺少c++ 11 std::to_string。但是,关于它缺少C函数重载的信息对我来说是陌生的。

In practice the lacking overloads with g++ means

在实践中,用g++的方法来进行重载。

  • ignoring the g++ issue, or

    忽略g+问题,或者

  • avoiding using the missing g++ overloads,
    e.g. using only double sin( double ), or

    避免使用缺失的g++重载,例如只使用双sin(双sin)或

  • using the std namespace overloads
    (one then needs to include <cmath> to guarantee their presence with g++).

    使用std名称空间重载(然后需要包含 以保证它们与g++在一起)。

In order to use the g++ std namespace overloads unqualified, one practical approach is to define headers wrappers for this compiler. I've used that approach to address g++ shortcomings wrt. to the printf family. For as David Wheeler once remarked, “All problems in computer science can be solved by another level of indirection”…

为了使用g++ std命名空间重载非限定的,一种实用的方法是为这个编译器定义header包装器。我已经使用这种方法来解决g+缺点wrt。printf的家庭。正如大卫·惠勒曾经说过的,“计算机科学中的所有问题都可以通过另一种间接的方式来解决”……

Then things can be arranged so that standard code that uses g++'s missing overloads, also compiles with g++. This adjusts the compiler to the standard, with a fixed amount of code.

然后可以安排一些事情,以便使用g++丢失的重载的标准代码也可以使用g++进行编译。这将编译器调整到标准,并使用一定数量的代码。

#2


7  

Just like any other header file, you #include <cassert> when you use something declared in that header file, such as assert().

就像任何其他头文件一样,当您使用头文件中声明的内容(如assert()时,您的#包含

#3


0  

See an easily accessible reference

查看一个容易访问的引用。

#include <iostream>
// uncomment to disable assert()
// #define NDEBUG
#include <cassert>

int main()
{
    assert(2+2==4);
    std::cout << "Execution continues past the first assert\n";
    assert(2+2==5);
    std::cout << "Execution continues past the second assert\n";
}

#4


0  

assert.h defines one macro function that can be used as a standard debugging tool.

断言。h定义了一个宏函数,可以用作标准的调试工具。

#1


40  

In short, don't use it; use <assert.h>.

简而言之,不要使用它;使用< assert.h >。

C++11 removed any formal guarantee of a "c...." header not polluting the global namespace.

c++ 11删除任何正式的保证一个“C ....”头不污染全局命名空间。

It was never an in-practice guarantee, and now it's not even a formal guarantee.

这从来都不是实际的担保,现在甚至也不是正式的担保。

Hence, with C++11 there is no longer any conceivable advantage in using the "c...." header variants, while there is the distinct and clear disadvantage that code that works well with one compiler and version of that compiler, may fail to compile with another compiler or version, due to e.g. name collisions or different overload selection in the global namespace.

因此,与c++ 11不再有任何可能的优势在使用“C ....”头变异,虽然是截然不同的和明确的缺点,运行良好的代码编译器和编译器的版本,可能无法与另一个编译器或编译版本,由于如名称冲突或不同过载选择全局命名空间。

SO, while cassert was pretty meaningless in C++03 (you can't put a macro in a namespace), it is totally meaningless -- even as a special case of a general scheme -- in C++11.

因此,虽然cassert在c++ 03中毫无意义(您不能将宏放在名称空间中),但在c++ 11中,它是完全没有意义的——即使作为通用方案的一个特例。


Addendum, Dec 22 2013:

附录,2013年12月22日:

The standard defines each C++ C header <X.h> header in terms of the <cX> header, which in turn is defined in terms of the corresponding C library header.

标准定义了每个c++ C头 标头以 标头的形式表示,而 标头又以相应的C库标头定义。 。h>

C++11 §D.5/2:

c++ 11§D.5/2:

“Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope.”

“每个C标头,每个标头都有表单名的名称。h的行为就好像每个由相应的cname头放在标准库名称空间中的名称都放在全局名称空间范围内一样。

C++11 §D.5/3 (non-normative example):

c++ 11§D。5/3(非规范化的例子):

“The header <cstdlib> assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std.”

header 肯定地在名称空间std中提供了它的声明和定义。标题< stdlib。h>无疑地在全局命名空间中提供了相同的声明和定义,就像在C标准中一样。它还可以在名称空间std中提供这些名称。

Stack Overflow user C.R.’s comment made me aware that some versions of g++, such as MinGW g++ 4.7.2, are quite non-standard with respect to the <X.h> headers, lacking the overloads of e.g. sin that the C++ standard requires:

Stack Overflow用户C.R.他的评论让我意识到,g+的一些版本,比如MinGW g+ 4.7.2,对于 页眉,缺少c++标准要求的sin等重载: 来说是非常不标准的。h>

I already knew that MinGW g++ 4.7.2 also entirely lacks functions such as swprintf, and that it has ditto shortcomings in the pure C++ library such as lacking C++11 std::to_string. However, the information about it lacking the C function overloads was new to me.

我已经知道MinGW g+ 4.7.2也完全没有swprintf这样的函数,并且在纯c++库中也有同样的缺点,比如缺少c++ 11 std::to_string。但是,关于它缺少C函数重载的信息对我来说是陌生的。

In practice the lacking overloads with g++ means

在实践中,用g++的方法来进行重载。

  • ignoring the g++ issue, or

    忽略g+问题,或者

  • avoiding using the missing g++ overloads,
    e.g. using only double sin( double ), or

    避免使用缺失的g++重载,例如只使用双sin(双sin)或

  • using the std namespace overloads
    (one then needs to include <cmath> to guarantee their presence with g++).

    使用std名称空间重载(然后需要包含 以保证它们与g++在一起)。

In order to use the g++ std namespace overloads unqualified, one practical approach is to define headers wrappers for this compiler. I've used that approach to address g++ shortcomings wrt. to the printf family. For as David Wheeler once remarked, “All problems in computer science can be solved by another level of indirection”…

为了使用g++ std命名空间重载非限定的,一种实用的方法是为这个编译器定义header包装器。我已经使用这种方法来解决g+缺点wrt。printf的家庭。正如大卫·惠勒曾经说过的,“计算机科学中的所有问题都可以通过另一种间接的方式来解决”……

Then things can be arranged so that standard code that uses g++'s missing overloads, also compiles with g++. This adjusts the compiler to the standard, with a fixed amount of code.

然后可以安排一些事情,以便使用g++丢失的重载的标准代码也可以使用g++进行编译。这将编译器调整到标准,并使用一定数量的代码。

#2


7  

Just like any other header file, you #include <cassert> when you use something declared in that header file, such as assert().

就像任何其他头文件一样,当您使用头文件中声明的内容(如assert()时,您的#包含

#3


0  

See an easily accessible reference

查看一个容易访问的引用。

#include <iostream>
// uncomment to disable assert()
// #define NDEBUG
#include <cassert>

int main()
{
    assert(2+2==4);
    std::cout << "Execution continues past the first assert\n";
    assert(2+2==5);
    std::cout << "Execution continues past the second assert\n";
}

#4


0  

assert.h defines one macro function that can be used as a standard debugging tool.

断言。h定义了一个宏函数,可以用作标准的调试工具。