C ++:在另一个函数中声明一个函数不是编译器错误,所以...这是什么? [英] C++: declaration of a function inside another function is not a compiler error so... what is it?

查看:49
本文介绍了C ++:在另一个函数中声明一个函数不是编译器错误,所以...这是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

偶然地,我编译了一个与此类似的资源:

 //void y();//可选地声明y()无效x(){//一些代码...无效y();//一些代码...}//void y(){/*某些代码*/}//可选地定义y() 

这是使用VS 2017和两个clang版本进行编译的.这些编译器都没有抱怨此源代码–没有错误,没有警告.我的代码中有错别字–在此示例中, y()前面不应有 void ,因此 y()应该被调用,所以它是一个相当偷偷摸摸的错误,在编译时没有明显的后果.

我很好奇编译器在想什么.为了调试代码,无法访问 x()中的 void y(); 行,因此似乎没有实际的机器代码生成.我用上面声明的 void y(){/* somecode */} 函数对此函数进行了测试,并在 x()下面定义了该函数,也没有这样的函数–没什么区别./p>

编译器是否将此声明视为声明?如果是这样,那么在C ++中不允许以这种方式(不谈论lambda)在函数中定义函数时如何进一步使用它?我假设,如果这样的声明在函数内部有效,那么如果一个人也想在 x()内定义一个函数,那将是最有意义的,否则,在 y()可以在 x()的上方和外部移动.

相关讨论和解释:

解决方案

我很好奇编译器在想什么.

它认为您以名称 y 声明了一个函数,该函数返回 void 并具有一个空的参数列表. void y(); 是一个函数声明.

编译器是否认为这是一个声明?

是的

如果是这样,当C ++中不允许使用嵌套函数时,该如何进一步使用?

例如,可以使用函数声明来调用它或获取其地址.像这样:

  void x(){无效y();//函数声明//现在已经声明了y,可以将其称为:y();//函数调用//同样,现在可以使用该地址:void(* fun_ptr)()= y;} 

Accidentally I compiled a source similar to this:

//void y(); //optionally declaring y()

void x()
{
  //some code...
  void y();
  //some code...
}

//void y() {/*some code*/} //optionally defining y()

This was compiled with VS 2017 and also two clang versions. None of these compilers complained about this source code – no errors, no warnings. There was a typo in my code – in this example, there should be no void in front of y() and therefore y() was supposed to be called, so it was a rather sneaky bug with no visible consequences while compiling.

I was curious what the compiler was thinking. Trying to debug the code, the void y(); line inside x() was inaccessible, so seems like no actual machine code was produced. I tested this with the void y() {/*somecode*/} function declared above and defined below x() and also with no such function – no difference.

Did the compilers consider this a declaration? if so, how this could be further used when defining a function within a function this way (not speaking about lambdas) is not allowed in C++? I assume that if such a declaration is valid inside a function, it would mostly make sense if one wanted to define a function inside x() as well, otherwise the declaration of y() could be moved outside and above x().

EDIT: Related discussions and explanations:

解决方案

I was curious what the compiler was thinking.

Its thinking that you declared a function by the name y that returns void and has an empty argument list. void y(); is a function declaration.

Did the compiler(s) consider this a declaration?

Yes.

if so, how this could be further used when nested functions are not allowed in C++?

You can use a function declaration for example to call it, or to take its address. Like so:

void x()
{
    void y(); // a function declaration

    // now that y has been declared, it can be called:
    y();      // a function call

    // also, it is now possible to take the address:
    void (*fun_ptr)() = y;
}

这篇关于C ++:在另一个函数中声明一个函数不是编译器错误,所以...这是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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