简体   繁体   English

function 定义中 function 声明的目的

[英]Purpose of function declaration within function definition

In C, one can of course declare and define functions.在 C 中,当然可以声明和定义函数。 And this interacts with default constructor calls from C++ to yield the most vexing parse problem.这与来自 C++ 的默认构造函数调用交互,产生最棘手的解析问题。 Say I want to declare a function foo , define a function bar and create an instance t , I might mistakenly write this (having Python or Java in mind): Say I want to declare a function foo , define a function bar and create an instance t , I might mistakenly write this (having Python or Java in mind):

T foo();

T bar() {
    T t();
}

This will not do what I want, it will declare a function t with no arguments (or perhaps arbitrary many ones as there is no void written in there, not sure) and return type T .这不会做我想要的,它将声明一个 function t没有 arguments (或者可能是任意多个,因为那里没有写void ,不确定)并返回类型T I would get a T instance with T t;我会得到一个带有 T t 的T实例T t; . .

Why is this syntax even allowed?为什么甚至允许这种语法? What is the point of declaring a function just within a function?在 function 中声明 function 有什么意义? The linker will have to resolve this at link time anyway, and then one could just declare that function in the file scope. linker 无论如何都必须在链接时解决这个问题,然后可以在文件 scope 中声明 function。 Is there some edge case that I am missing?是否有一些我遗漏的边缘案例?

Not a formal explanation but just a use-case in which I would use the function-scope declaration.不是正式的解释,而只是我将使用函数范围声明的用例。

Imagine you have a C header file offering an inline static function, but in specific cases this function should rely on an internal helper function which is not part of the API. Imagine you have a C header file offering an inline static function, but in specific cases this function should rely on an internal helper function which is not part of the API.

// file utils.h
#ifndef UTILS_H
#define UTILS_H

// many other things...

inline static
int
nice_util(int i,
          int j)
{
  int k=i+2*j;
  if(i>10) // should not happen so often
  {
    void ugly_helper_function(int n); // no one else should be aware of this function
    ugly_helper_function(k);
  }
  return k;
}

#endif // UTILS_H

There is probably an implementation file coming with this header-file.这个头文件可能有一个实现文件。

// file utils.c

#include <utils.h>
#include <stdio.h>

// many other things...

void
ugly_helper_function(int n)
{
  printf("%s(%d)\n", __func__, n);
}

Using this header-file provides the expected function but not the internal one (except if you ignore the warnings).使用此头文件可提供预期的 function 但不是内部的(除非您忽略警告)。

// file prog.c
#include <utils.h>

int
main(void)
{
  // ugly_helper_function(5678); // warning: implicit declaration...
  return nice_util(12, 34);
}

Without the ability to declare the internal function at function-scope, we would have had to declare it at global-scope in the header file, and that would have made it accessible without any warning.如果无法在函数范围内声明内部function,我们将不得不在 header 文件中的全局范围内声明它,这样就可以在没有任何警告的情况下访问它。

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

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