English 中文(简体)
是否可将具有最复杂论点的职能点用作具有非最根本理由的职能点?
原标题:Can a function pointer with a const argument be used as a function pointer with a nonconst argument?

Perhaps the title isn t clear in itself... I have a function f (provided by some library) that takes as an argument a function pointer of signature void g(int*), i.e.

void f(void (*g)(int*));

然而,我愿利用<条码>g(我定义的)签字<条码>(避免g(const int*)。 事先,我可以看一看这如何违反任何最正确之处,因为所有签署<代码>f。 也就是说,g将只打上(无<条码>const>int*(无<条码>const),而且实际上,我可以称之为“<条码>,避免(最有条码的)功能,但无<条码>。

但是,海湾合作委员会抱怨说,

expected  void (*)(int *) , but argument is of type  void (*)(const int *) 

我看不出这一申诉如何合法,任何人都知道我是否理解这是错误的,或者是否有办法解决这一问题?

最佳回答

你们似乎发现,汇编作者和标准作者没有做什么。 摘自C99第1256条草案,第6.7.5.3条第15款

相应的参数应具有相容的类型。

注:const int * is notbian with int *。 但是,<编码>int *可改为const int *。 第6.32.3节,第2段

对于任何限定的类别,可以将非q>的点子转换为点子到q合格型号。

如果从同一类合格或无保留版本衍生的替代品类型可以接受,则更复杂的仲裁规则就根本不属于标准。 因此,你的法典在技术上违反了标准。

www.un.org/Depts/DGACM/index_french.htm 在我看来,这一错误应当由汇编者视为“固定”:你的代码并不从技术上说是符合标准,但含义明确,该守则绝对安全。 感到可以自由地向您的汇编商供应商发出特别要求。 有许多不符合同的做法,没有<代码>-pedantic生成警报。

作为最后说明,我与Clang编辑,汇编者告诉我,警告是 pe的。 然而,我没有要求发出警示......因此似乎没有办法去掉。

warning: incompatible pointer types passing  void (int const *) , expected  void (*)(int *) 
      [-pedantic]

<<>Workaround>: 采用明确的措辞。

void g(const int *);

f((void (*)(int *)) g);
问题回答

You are right, there s no reason C should disallow that call (other than because the C standard says it should). U(*)(T*) should be a sub-type of U(*)(const T*) because int* is a sub-type of const int* through substitutability.

为什么C不允许这样做,我不知道。

As for work-arounds, you can provide a proxy function:

void foo(const int* x) { ... } // <-- the function you want to pass in

void bar(int* x) { foo(x); } // proxy

f(bar); // instead of f(foo)

采用安全、符合标准的替代方式,就足以证明这一呼吁首先应当有效。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签