当我调用函数而不将其返回值赋给变量时会发生什么? [重复]

时间:2021-12-14 04:12:55

This question already has an answer here:

这个问题在这里已有答案:

Say I have the following function:

说我有以下功能:

int foo()
{
    return 1234;
}

What is the effect of calling this function without assigning its returned value to any variable:

调用此函数而不将其返回值赋给任何变量有什么影响:

foo();

Also what is the effect of using an operator without assigning its returned value to any variable:

使用运算符而不将其返回值赋给任何变量的影响是什么:

1 + 2 + 3;

4 个解决方案

#1


5  

In C++ an expression is a valid statement; the computed value is simply discarded.

在C ++中,表达式是一个有效的语句;简单地丢弃计算值。

Some compilers are smart enough to inform you with a warning if the code looks strange; for example having:

如果代码看起来很奇怪,有些编译器足够聪明,可以通知您一个警告;例如:

foo(1,2,3);

can make sense (calls the function that probably has side effects) but code like

可以有意义(调用可能有副作用的功能)但代码就像

x < 3;

is not useful because it has no defined side effect and the value is discarded.

没有用,因为它没有定义的副作用,并且该值被丢弃。

In g++ there also some library functions that despite having side effects are marked specifically so that ignoring the return value raises a warning (like fread) because it's a sign of a bug waiting to bite.

在g ++中,还有一些库函数,尽管有副作用,但具体标记为忽略返回值会引发警告(如fread),因为它是等待咬的bug的标志。

There are also very common cases in which ignoring the result of a function is done frequently (like printf) and in which expressions that return a value are used only for the side effect (like std::cout << "Hello";).

还有一些常见的情况是忽略函数的结果(如printf),并且返回值的表达式仅用于副作用(如std :: cout <<“Hello”;)。

If you want to enforce at run time that the result of a function has not been silently ignored there is a trick that sometimes can be used:

如果你想在运行时强制执行函数的结果没有被忽略,那么有时可以使用一个技巧:

template<typename T>
struct must_check {
    T x;
    bool checked;
    must_chec(const T& x) : x(x), checked(false) {}
    operator T () { checked = true; return x; }
    ~must_check() {
        if (!checked)
          throw std::runtime_error("Result value has not been checked");
    }
};

Now instead of writing

现在而不是写作

int square(int x) {
    return x * x;
}

just write

must_check<int> square(int x) {
    return x * x;
}

Then code like

那么代码就像

square(12);

will throw an exception at runtime, while

将在运行时抛出异常,而

int result = square(12);

will work fine.

会很好的。

This is possible but is not a really good idea however, because throwing an exception in a destructor is a really bad behavior that most often results in terminating a program abnormally (the reason is that sometimes destructors are called automatically because of an exception, throwing another exception during that phase is a lethal move).

这是可能的,但不是一个非常好的主意,因为在析构函数中抛出异常是一种非常糟糕的行为,通常导致程序异常终止(原因是有时析构函数会因异常而自动调用,抛出另一个异常在该阶段的例外是致命的举动)。

#2


1  

The operation or function will be evaluated and will continue running. Depending on your compiler, you may see a warning but it will otherwise continue running without issue.

将评估操作或功能并继续运行。根据您的编译器,您可能会看到警告,但否则它将继续运行而不会出现问题。

#3


0  

You are not obliged to do anything with a function's returned value in C++. You can call function and simply ignore the returned value. Operator is also function, thus you can call it, then expression will be evaluated and again - result may be just forgotten.

您没有义务在C ++中使用函数的返回值执行任何操作。您可以调用函数并简单地忽略返回的值。运算符也是函数,因此您可以调用它,然后表达式将被再次评估 - 结果可能只是被遗忘。

#4


0  

At a low level,when a function returns value,this value is copied into eax(on 32-bit machine) or rax(64-bit machine) register,then caller function uses eax or rax register to read returned data. In this case caller function just doesn't read eax/rax registers.

在低级别,当函数返回值时,该值被复制到eax(在32位机器上)或rax(64位机器)寄存器中,然后调用函数使用eax或rax寄存器来读取返回的数据。在这种情况下,调用函数只是不读取eax / rax寄存器。

#1


5  

In C++ an expression is a valid statement; the computed value is simply discarded.

在C ++中,表达式是一个有效的语句;简单地丢弃计算值。

Some compilers are smart enough to inform you with a warning if the code looks strange; for example having:

如果代码看起来很奇怪,有些编译器足够聪明,可以通知您一个警告;例如:

foo(1,2,3);

can make sense (calls the function that probably has side effects) but code like

可以有意义(调用可能有副作用的功能)但代码就像

x < 3;

is not useful because it has no defined side effect and the value is discarded.

没有用,因为它没有定义的副作用,并且该值被丢弃。

In g++ there also some library functions that despite having side effects are marked specifically so that ignoring the return value raises a warning (like fread) because it's a sign of a bug waiting to bite.

在g ++中,还有一些库函数,尽管有副作用,但具体标记为忽略返回值会引发警告(如fread),因为它是等待咬的bug的标志。

There are also very common cases in which ignoring the result of a function is done frequently (like printf) and in which expressions that return a value are used only for the side effect (like std::cout << "Hello";).

还有一些常见的情况是忽略函数的结果(如printf),并且返回值的表达式仅用于副作用(如std :: cout <<“Hello”;)。

If you want to enforce at run time that the result of a function has not been silently ignored there is a trick that sometimes can be used:

如果你想在运行时强制执行函数的结果没有被忽略,那么有时可以使用一个技巧:

template<typename T>
struct must_check {
    T x;
    bool checked;
    must_chec(const T& x) : x(x), checked(false) {}
    operator T () { checked = true; return x; }
    ~must_check() {
        if (!checked)
          throw std::runtime_error("Result value has not been checked");
    }
};

Now instead of writing

现在而不是写作

int square(int x) {
    return x * x;
}

just write

must_check<int> square(int x) {
    return x * x;
}

Then code like

那么代码就像

square(12);

will throw an exception at runtime, while

将在运行时抛出异常,而

int result = square(12);

will work fine.

会很好的。

This is possible but is not a really good idea however, because throwing an exception in a destructor is a really bad behavior that most often results in terminating a program abnormally (the reason is that sometimes destructors are called automatically because of an exception, throwing another exception during that phase is a lethal move).

这是可能的,但不是一个非常好的主意,因为在析构函数中抛出异常是一种非常糟糕的行为,通常导致程序异常终止(原因是有时析构函数会因异常而自动调用,抛出另一个异常在该阶段的例外是致命的举动)。

#2


1  

The operation or function will be evaluated and will continue running. Depending on your compiler, you may see a warning but it will otherwise continue running without issue.

将评估操作或功能并继续运行。根据您的编译器,您可能会看到警告,但否则它将继续运行而不会出现问题。

#3


0  

You are not obliged to do anything with a function's returned value in C++. You can call function and simply ignore the returned value. Operator is also function, thus you can call it, then expression will be evaluated and again - result may be just forgotten.

您没有义务在C ++中使用函数的返回值执行任何操作。您可以调用函数并简单地忽略返回的值。运算符也是函数,因此您可以调用它,然后表达式将被再次评估 - 结果可能只是被遗忘。

#4


0  

At a low level,when a function returns value,this value is copied into eax(on 32-bit machine) or rax(64-bit machine) register,then caller function uses eax or rax register to read returned data. In this case caller function just doesn't read eax/rax registers.

在低级别,当函数返回值时,该值被复制到eax(在32位机器上)或rax(64位机器)寄存器中,然后调用函数使用eax或rax寄存器来读取返回的数据。在这种情况下,调用函数只是不读取eax / rax寄存器。