未使用的函数返回值会怎样?

2022-01-19 00:00:00 function windows return c++

如果我有一个程序:

#include <iostream>

using namespace std;

int TestIntReturn(int &x, int &y)
{
    x = 1000;
    y = 1000;
    return x+y;
}

int main()
{
    int a = 1;
    int b = 2;
    cout << a << " " << b << endl;
    TestIntReturn(a,b);
    cout << a << " " << b << endl;
}

TestInReturn(a,b) 的返回值没有被使用会怎样?

what happens to the return value of TestInReturn(a,b) since it is unused?

推荐答案

它被丢弃;表达式 TestInReturn(a,b) 是一个丢弃值表达式.丢弃 int 无效,但丢弃 volatile int(或任何其他 volatile 限定类型)可能会产生从内存读取的效果.

It is discarded; the expression TestInReturn(a,b) is a discarded-value expression. Discarding an int has no effect, but discarding a volatile int (or any other volatile-qualified type) can have the effect of reading from memory.

相关文章