int(*)(int *)= 5(或任何整数值)的含义 [英] Meaning of int (*) (int *) = 5 (or any integer value)

查看:114
本文介绍了int(*)(int *)= 5(或任何整数值)的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解决这个问题:

int main() {
    int (*) (int *) = 5;
    return 0;
}

以上赋值可使用g ++ c ++ 11进行编译.我知道int (*) (int *)是指向接受(int *)作为参数并返回一个int的函数的指针,但是我不知道如何将其等同于5.起初,我认为它是一个不断返回5的函数. (根据我最近在F#中的学习,可能是哈哈),然后我短暂地想到了函数指针指向内存位置5,但这显然不起作用,十六进制值也不起作用.

认为可能是因为该函数返回一个int,并且可以(以某种方式)分配一个int是可以的,所以我也尝试这样做:

int * (*) (int *) = my_ptr

其中,my_ptr的类型为int *,与第二个函数指针的类型相同,第一种情况为int类型.这不会编译.分配5或任何int值代替my_ptr,也不会为此函数指针编译.

那任务是什么意思?

更新1

我们已确认这是一个错误,如最佳答案所示.但是,仍然不知道分配给函数指针的值实际上是发生是什么,或者分配发生了什么.任何(良好)的解释将不胜感激!请参阅下面的编辑内容,以更清楚地了解该问题.

编辑1

我正在使用gcc版本4.8.2(在Ubuntu 4.8.2中)

编辑2

实际上,将其等同于对我的编译器有效的任何东西.甚至将其等同于std :: string变量,或者返回双精度值的函数名称也可以使用.

编辑2.1

有趣的是,将其作为指向任何返回非指针数据类型的函数的函数指针,将使其进行编译,例如

std::string (*) () = 5.6;

但是一旦函数指针指向返回某些指针的函数,它就不会编译,例如使用

some_data_type ** (*) () = any_value;

解决方案

这是g ++中的错误.

 int (*) (int *) 

是类型名称.

在C ++中,不能有类型名称没有标识符的声明.

所以这可以用g ++编译.

 int (*) (int *) = 5;

这也可以编译:

 int (*) (int *);

但它们都是无效的声明.

编辑:

TC 在注释中提及bugzilla错误 解决方案

It's a bug in g++.

 int (*) (int *) 

is a type name.

In C++ you cannot have a declaration with a type name without an identifier.

So this compiles with g++.

 int (*) (int *) = 5;

and this compiles as well:

 int (*) (int *);

but they are both invalid declarations.

EDIT:

T.C. mentions in the comments bugzilla bug 60680 with a similar test case but it has not yet been approved. The bug is confirmed in bugzilla.

EDIT2:

When the two declarations above are at file scope g++ correctly issues a diagnostic (it fails to issue the diagnostic at block scope).

EDIT3:

I checked and I can reproduce the issue on the latest release of g++ version 4 (4.9.2), latest pre-release version 5 (5.0.1 20150412) and latest experimental version 6 (6.0.0 20150412).

这篇关于int(*)(int *)= 5(或任何整数值)的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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