C C++为什么需要声明?--其实是个历史原因的累赘, 完全没必要 。

https://stackoverflow.com/questions/645778/what-are-the-advantages-and-disadvantages-of-separating-declaration-and-definitio/645855#645855

Historically this was to help the compiler. You had to give it the list of names before it used them - whether this was the actual usage, or a forward declaration (C's default funcion prototype aside).

Modern compilers for modern languages show that this is no longer a necessity, so C & C++'s (as well as Objective-C, and probably others) syntax here is histotical baggage. In fact one this is one of the big problems with C++ that even the addition of a proper module system will not solve.

Disadvantages are: lots of heavily nested include files (I've traced include trees before, they are surprisingly huge) and redundancy between declaration and definition - all leading to longer coding times and longer compile times (ever compared the compile times between comparable C++ and C# projects? This is one of the reasons for the difference). Header files must be provided for users of any components you provide. Chances of ODR violations. Reliance on the pre-processor (many modern languages do not need a pre-processor step), which makes your code more fragile and harder for tools to parse.

Advantages: no much. You could argue that you get a list of function names grouped together in one place for documentation purposes - but most IDEs have some sort of code folding ability these days, and projects of any size should be using doc generators (such as doxygen) anyway. With a cleaner, pre-processor-less, module based syntax it is easier for tools to follow your code and provide this and more, so I think this "advantage" is just about moot.

从历史上看,这是为了帮助编译器。在使用它们之前你必须给它一个名单 - 无论是实际用法,还是前向声明(C的默认功能原型除外)。

现代语言的现代编译器表明这不再是必需品,因此这里的C&C ++(以及Objective-C,可能还有其他语法)语法是组织上的包袱。事实上,这是C ++的一大问题,即使添加适当的模块系统也无法解决。

缺点是:许多重度嵌套的包含文件(我之前跟踪过包含树,它们非常庞大)以及声明和定义之间的冗余 - 所有这些都导致更长的编码时间和更长的编译时间(比较可比较的C ++和C ++之间的编译时间) C#项目?这是差异的原因之一)。必须为您提供的任何组件的用户提供头文件。 ODR违规的可能性。依赖于预处理器(许多现代语言不需要预处理器步骤),这使您的代码更加脆弱,并且难以分析工具。

优点:没什么。你可能会争辩说你得到了一个在一个地方组合在一起的函数名列表以供记录 - 但是现在大多数IDE都有某种代码折叠能力,而且任何大小的项目都应该使用doc生成器(例如doxygen)。使用更清晰,无预处理器,基于模块的语法,工具更容易遵循您的代码并提供更多,因此我认为这种“优势”只是没有实际意义。

 

 

https://stackoverflow.com/questions/5876844/why-are-function-declaration-mandatory-in-c-and-not-in-c#

In a discussion that involves both C and C++ "function declaration" is a rather vague term. These languages are significantly different in this regard.

In C++ language there's only one kind of function declaration: declaration with all parameter types and return type. Such declarations are necessary because C++ language supports function overloading. In order to choose which function to call the compiler needs to know everything about the function and needs to know which overloaded versions of the function are available. If you "forget" to declare some overloaded version, it will not be considered by overload resolution. That is at least one of the reasons function declarations are necessary in C++.

In C language there are two kinds of function declarations: non-prototype declarations and prototype declarations (or simply prototypes). A prototype in C is pretty similar to C++ declaration - it includes all parameter types. Prototypes have always been required in standard C for variadic functions (functions with ... parameters). For non-variadic functions prototype declarations are not required even today. But starting from C99 at least non-prototype declarations are required for all other functions. In older C89/90 version of the language function declarations for non-variadic functions were not required.

So, that should basically answer your question. In C++ function declarations are required because language features rely on them critically. In modern C function declarations are also required just to make the code safer. In older versions of C function declarations were not required mostly simply because the language was defined to work without them.

在涉及C和C ++的讨论中,“函数声明”是一个相当含糊的术语。在这方面,这些语言有很大不同。

在C ++语言中,只有一种函数声明:声明包含所有参数类型和返回类型。这样的声明是必要的,因为C ++语言支持函数重载。为了选择调用哪个函数,编译器需要知道函数的所有内容,并且需要知道函数的哪些重载版本可用。如果您“忘记”声明某些重载版本,则不会通过重载解析来考虑它。这至少是C ++中必要的函数声明的原因之一。

在C语言中,有两种函数声明:非原型声明和原型声明(或简称原型)。 C中的原型与C ++声明非常相似 - 它包含所有参数类型。标准C中一直需要原型用于可变参数函数(具有...参数的函数)。对于非可变函数,即使在今天也不需要原型声明。但是从C99开始,所有其他函数都需要至少非原型声明。在较旧的C89 / 90版本的语言函数中,不需要非变量函数的声明。

所以,这应该基本上回答你的问题。在C ++中,函数声明是必需的,因为语言特性严格依赖于它们。在现代C函数中,还需要声明才能使代码更安全。在旧版本的C函数声明中,主要不需要声明,因为语言被定义为在没有它们的情况下工作。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值