为什么指向函数的指针等于1? [英] Why pointer to function is equal to 1?

查看:127
本文介绍了为什么指向函数的指针等于1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查以下代码:

#include <iostream>
using namespace std;

int& foo() {
    static int i = 0;
    return i;
}

int main() {
    cout << &foo() << endl;
    cout << &foo << endl;

    return 0;
}

如您所见,第一个cout打印foo()返回值的地址,该地址将是foo()内部的静态变量i.对于第二个cout,我期望&foo返回foo()函数的地址,如此处:

As you see, the first cout prints address of return value of foo() which will be static variable i inside foo(). For 2nd cout I was expecting that &foo returns address of foo() function, as stated here:

2)如果操作数是非静态成员的限定名称,例如 & C :: member,结果是指向成员函数的prvalue指针,或者 指向类C中类型T的数据成员的指针.请注意,& member都不 也不能使用C :: member甚至&(C :: member)来初始化 指向成员的指针.

2) If the operand is a qualified name of a non-static member, e.g. &C::member, the result is a prvalue pointer to member function or pointer to data member of type T in class C. Note that neither &member nor C::member nor even &(C::member) may be used to initialize a pointer to member.

但令我惊讶的是,这是我的输出:

But to my surprise, this is my output:

0x5650dc8dc174
1

第一个可以,但是第二个可以是1?这是怎么发生的?为了确保我没有弄乱任何东西,我在C中编写了这段代码:

First one is ok, but 2nd one is 1? How this happened? To make sure that I have not messed up anything, I wrote this code in C:

#include <stdio.h>

int foo() {
}

int main(void) {
    printf("%p", &foo);
    return 0;
}

具有以下输出:

0x55732bd426f0

可以正常工作.我是否错过了C++代码中的某些内容?还是因为内联foo函数(即使它不应该这样)?

which works as expected. Have I missed up something in C++ code? or maybe this is because of inlining foo function (even though it should not be like this)?

推荐答案

std::basic_ostream::operator<< 有两个重载,分别为boolconst void*;请注意,没有重载函数指针.

std::basic_ostream::operator<< has two overloads taking bool and const void*; note there's no overload taking function pointer.

basic_ostream& operator<<( bool value );        (6)   
basic_ostream& operator<<( const void* value ); (7)

对于int*和传递给std::basic_ostream::operator<<的函数指针,此处都需要隐式转换.

For both int* and function pointer passed to std::basic_ostream::operator<<, implicit conversions are required here.

通过int*时,选择(7)重载,因为隐式转换bool的转换要好. ="nofollow noreferrer">过载分辨率

When passing int*, the (7) overload is selected because the implicit conversion converting from int* to const void* is perferred than the one converting to bool in overload resolution,

如果两个转换序列因为具有 等级相同,则适用以下附加规则:

If two conversion sequences are indistinguishable because they have the same rank, the following additional rules apply:

1)转换涉及到bool的指针,指向成员的指针 bool或std :: nullptr_t转换为bool的效果比 不

1) Conversion that involves pointer to bool, pointer-to-member to bool, or std::nullptr_t to bool conversion is worse than the one that doesn't

指向任何(可选具有cv资格的)对象类型T的prvalue指针可以转换为指向(完全具有cv资格的)void的prvalue指针.结果指针与原始指针值在内存中的位置相同.

A prvalue pointer to any (optionally cv-qualified) object type T can be converted to a prvalue pointer to (identically cv-qualified) void. The resulting pointer represents the same location in memory as the original pointer value.

传递函数指针时,选择了(6)重载;函数指针可以隐式转换为bool ,但不能const void*.

When passing function pointer, the (6) overload is selected; function pointer can be converted to bool implicitly, but not to const void*.

整数,浮点数,无作用域枚举,指针, 指针类型和成员类型可以转换为类型的prvalue 布尔.

A prvalue of integral, floating-point, unscoped enumeration, pointer, and pointer-to-member types can be converted to a prvalue of type bool.

零值(对于整数,浮点数和无作用域) 枚举)以及null指针和null指向成员的指针 值变为假.所有其他值都变为true.

The value zero (for integral, floating-point, and unscoped enumeration) and the null pointer and the null pointer-to-member values become false. All other values become true.

这篇关于为什么指向函数的指针等于1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆