简体   繁体   English

为什么可以将 void(*)(int x, int y) 传递给期望 void(*)() 的 attachInterrupt?

[英]Why is it possible to pass a void(*)(int x, int y) to attachInterrupt that expects void(*)()?

I've been inattentive and for some reason created a function that takes two arguments, and I passed it to attachInterrupt like so:我一直不专心,出于某种原因创建了一个需要两个 arguments 的 function,然后我将它传递给attachInterrupt ,如下所示:

int state = 42;
void simplified_state_handler(){
    state++;
}

void interrupt_func(int x, int y) {
    simplified_state_handler();
}

attachInterrupt(digitalPinToInterrupt(10), interrupt_func, CHANGE);

The code got compiled with no complaints at all, and it even works.代码编译时没有任何抱怨,它甚至可以工作。 Now, a bit later, I really can't understand why.现在,过了一会儿,我真的不明白为什么。 Reading and digging the attachInterrupt code didn't help.阅读和挖掘attachInterrupt代码没有帮助。 Please explain why can I pass such a function at all.请解释为什么我完全可以通过这样的 function。 I'm keeping the (now) useless simplified_state_handler in the example, maybe it is important.我在示例中保留了(现在)无用的simplified_state_handler状态处理程序,也许它很重要。

The compiler settings of the AVR boards allow it. AVR 板的编译器设置允许它。

It is only a warning: invalid conversion from 'void (*)(int, int)' to 'void (*)()' [-fpermissive] .这只是一个warning: invalid conversion from 'void (*)(int, int)' to 'void (*)()' [-fpermissive]

On other Arduino platforms (SAMD, STM32, esp8266) it is an error.在其他 Arduino 平台(SAMD、STM32、esp8266)上,这是一个错误。

The compiler settings in AVR platform were benevolent from the start and they can't change them suddenly. AVR平台的编译器设置从一开始就很好,不能突然改变。 Many existing codes would not compile then.许多现有的代码将无法编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM