几个函数指针 [英] Pointers of several functions

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

问题描述

有没有保证,这只能由其名称(不参数和返回值类型也)不同的功能不能共享C和C ++相同的地址?我看不出在标准任何事情。

 的#include<&了cassert GT;无效美孚(){}空隙栏(){}诠释的main()
{
    断言(富=吧!);
}


解决方案

在C ++ 11标准说


  

5.10平等运营商结果
  同一类型(指针转换后)的指针可以平等进行比较。相同类型的两个指针比较相等当且仅当它们都为空,都指向相同的功能,或两者重新present相同的地址(3.9.2)。


如果你没有任何指针的函数,他们只可能具有相同的地址,但我们不知道。如果你的的比较指针两种不同的功能,它们不能比较平等的。


混乱的一个原因可能是MSVC的编译器是众所周知的code为发生在生产用于不同类型相同的机器code(如 INT )。这是不符合。

不过,这是与的不同的的签名,而不是完全这个问题是关于什么的。

功能

Is there any guarantees that the functions which differs only by its names (not parameters and return type also) can't share the same address in C and C++? I don't see anything about it in the standard.

#include <cassert>

void foo() {}

void bar() {}

int main()
{
    assert(foo != bar);
}

解决方案

The C++11 standard says

5.10 Equality operators
Pointers of the same type (after pointer conversions) can be compared for equality. Two pointers of the same type compare equal if and only if they are both null, both point to the same function, or both represent the same address (3.9.2).

If you don't have any pointers to the functions, they just might have the same address, but we wouldn't know. If you are comparing pointers to two different functions, they must not compare equal.


One cause for confusion might be that the MSVC compilers are known to combine code for template functions that happen to produce identical machine code for different types (like int and long). This is not compliant.

However, this is for functions with different signatures, and not exactly what this question is about.

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

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