你可以将函数用作if语句的条件,而不调用它吗?

huangapple go评论61阅读模式
英文:

Why can you use a function as an if-statement condition without calling it?

问题

条件(1)尽管在if语句中没有传递变量number到函数check_number中,但会打印出Hello world!

在情况(2)中,我得到了预期的not hello world

英文:

I was wondering why both versions of this code have no error:

#include <iostream>

bool check_number(int n) {
    return n < 5;
}

int main() {
    int number = 6;

    // (1) prints "Hello World"
    if (check_number)
    // (2) prints "not hello world"
    if (check_number(number))
    {
        std::cout << "Hello world!";
    }
    else
    {
        std::cout << "not hello world";
    }

    return 0;
}

Condition (1) prints Hello world! despite the fact that I have not passed the variable number into the function check_number in the if-statement.

In case (2), I get not hello world as expected.

答案1

得分: 9

if (check_number) {只是检查函数指针的值是否为nullptr(或0和其他空指针常数),这将始终为true。它不会调用函数。

英文:

if (check_number) { is just checking the value of the function pointer against nullptr (or 0 and other null pointer constants), which is always going to be true. It is not calling the function.

答案2

得分: 5

if (check_number)

在这个上下文中,check_number 会衰减为函数指针,因为前者有一个地址,所以后者不是空指针,因此测试成功,进入了 if 分支。

这与将函数分配给函数指针而不明确获取地址的情况是一样的:

bool (*fPtr)(int) = check_number; // 不需要 &check_number
英文:
if (check_number)

In this context check_number decays to a function pointer, and as the former has an address, the latter is not the null pointer, thus the test succeeds and you enter the if branch.

That's just the same as if you assign the function to a function pointer without explicitly taking the address of:

bool(*fPtr)(int) = check_number; // don't need &check_number

答案3

得分: 5

从C++ 17标准(7.3函数指针转换)

1 函数类型T的lvalue可以转换为类型为“指向T的指针”的prvalue。结果是函数的指针。

和(7.14布尔转换)

1 算术、无范围枚举、指针或指向成员类型的prvalue可以转换为bool类型的prvalue。零值、空指针值或空成员指针值被转换为false;任何其他值被转换为true。对于直接初始化(11.6),std::nullptr_t类型的prvalue可以转换为bool类型的prvalue;结果值为false。

因此,在if语句的表达式中

if (check_number) {

函数标识符会被隐式转换为函数指针,因为它不是空指针,所以表达式的值为true。

英文:

From the C++ 17 Standard (7.3 Function-to-pointer conversion)

> 1 An lvalue of function type T can be converted to a prvalue of type
> “pointer to T”. The result is a pointer to the function.

and (7.14 Boolean conversions)

> 1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
> to member type can be converted to a prvalue of type bool. A zero
> value, null pointer value, or null member pointer value is converted
> to false; any other value is converted to true. For
> direct-initialization (11.6), a prvalue of type std::nullptr_t can be
> converted to a prvalue of type bool; the resulting value is false.

So in the expression of the if statement

if (check_number) {

the function designator is implicitly converted to a pointer to the function and as it is not a null pointer then the expression evaluates to true.

答案4

得分: 0

你可以推断,条件 (check_number) 不涉及函数调用,因为没有传递参数,正如你所观察到的那样。

那么这种情况怎么能够有效并表现为 true(即非零)的条件呢?

仅仅因为一个函数名本身,就像一个数组名本身一样,会被转换成一个指针(指向函数或数组)。

英文:

You can infer that the condition (check_number) does not involve a function call because no argument is passed, as you aptly observed.

So in what way can this be valid and behave as a true (i.e. nonzero) condition ?

Just by the fact that a function name alone, the same way as an array name alone, is turned to a pointer (to function or to array).

huangapple
  • 本文由 发表于 2023年7月13日 20:30:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定