简体   繁体   English

何时在作用域中评估 assert ( ) 表达式?

[英]When is the assert ( ) expression evaluated in a scope?

I was trying to understand the assert() macro in C++ and I am confused as to when the assert statement is checked for its validity.我试图理解 C++ 中的assert()宏,但我对何时检查 assert 语句的有效性感到困惑。 I created a class Pyramid where I wanted to check if the Class attributes are positive and so I created a try() -> catch() exception handling first and if I enter a negative value for instantiating a Pyramid Object, it throws the error( along with the printData() also being evaluated)我创建了一个 Pyramid 类,我想在其中检查 Class 属性是否为正,因此我首先创建了一个try() -> catch()异常处理,如果我输入一个负值来实例化一个 Pyramid 对象,它会抛出错误(连同printData()也被评估)

However is I go for assert() also by putting a statement caught=false in caught() { } the, neither the expression std::cout<<errorText nor the printData() gets evaluated and the program just throws an Assertion "caught" failed.不过是我去assert()也通过把一份声明中caught=falsecaught() { }的,无论是表达std::cout<<errorText也不printData()获取评估和计划只是抛出一个Assertion "caught" failed. error.错误。

Can someone please explain how is the control executed when we put an assert() statement in the scope and why std::cout<<errorText nor the printData() are not getting evaluated at all?有人可以解释一下当我们在作用域中放置assert()语句时控件是如何执行的,以及为什么std::cout<<errorTextprintData()根本没有得到评估? The code is as below:代码如下:

#include <stdexcept>
#include <iostream>
#include<string>
using namespace std;

class Pyramid
{
private:
    int length;
    int width;
    int height;
    float volume;

public:
    Pyramid(int l, int w, int h) : length(l), width(w), height(h)
    {
        volume = (1.0f / 3) * length * width * height;
    }

    void printData() const
    {
        std::cout << "\nLength : " << length
                  << "\nWidth : " << width
                  << "\nHeight : " << height
                  << "\nVolume : " << volume;
    }
    int Length() const
    {
        return length;
    }
    int Height() const
    {
        return height;
    }
    int Width() const
    {
        return width;
    }
    float Volume() const
    {
        return volume;
    }
};

int main()
{

    bool caught{true};
    try
    {   //Initialize Pyramid
        Pyramid pyramid( -11, 2, 3);

        //Print Check Pyramid Attributes 
        pyramid.printData();

        //Check for validity of attributes
        if (pyramid.Length() <= 0 || pyramid.Width() <= 0 || pyramid.Height() <= 0)
            throw std::string("\nAttributes cannot be zero or negative");
    }
    catch (std::string errorText)
    {
        std::cout << errorText;
        caught = false;
    }

    assert(caught);

    return 0;
}

When is the assert ( ) expression evaluated in a scope?何时在作用域中评估 assert ( ) 表达式?

assert is evaluated when the execution reaches it.当执行到达它时评估assert Same as most expression statements ( assert itself is a macro, but it will expand into an expression statement).与大多数表达式语句相同( assert本身是一个宏,但它会扩展为表达式语句)。

neither the expression std::cout<<errorText nor the printData() gets evaluated表达式 std::cout<<errorText 和 printData() 都没有被评估

You've assumed wrongly.你猜错了。 They will get evaluated.他们将得到评估。

What may happen instead, is that because the program is terminated, there is no cleanup of static objects.相反,可能会发生的情况是,因为程序终止,所以没有清理静态对象。 And because there is no cleanup, std::cout is not destroyed.因为没有清理, std::cout不会被销毁。 And because std::cout isn't destroyed, the output that you have inserted is not necessarily flushed to the standard output stream and instead remains in the buffer of std::cout .并且因为std::cout没有被破坏,您插入的输出不一定刷新到标准输出流,而是保留在std::cout的缓冲区中。 In which case you wouldn't see the output in the terminal.在这种情况下,您不会在终端中看到输出。

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

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