函数和结构体的前置什么是否必须? #18
Replies: 10 comments 3 replies
-
Wikipedia: Forward declarationIn computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition. Wikipeida: Function prototypeIn computer programming, a function prototype or function interface is a declaration of a function that specifies the function’s name and type signature (arity, data types of parameters, and return type), but omits the function body. While a function definition specifies how the function does what it does (the "implementation"), a function prototype merely specifies its interface, i.e. what data types go in and come out of it. The term "function prototype" is particularly used in the context of the programming languages C and C++ where placing forward declarations of functions in header files allows for splitting a program into translation units, i.e. into parts that a compiler can separately translate into object files, to be combined by a linker into an executable or a library. In a prototype, parameter names are optional (and in C/C++ have function prototype scope, meaning their scope ends at the end of the prototype), however, the type is necessary along with all modifiers (e.g. if it is a pointer or a reference to const parameter) except const alone. In object-oriented programming, interfaces and abstract methods serve much the same purpose. |
Beta Was this translation helpful? Give feedback.
-
有关于前置声明的讨论,可见于Google开发的新语言 -- Carbon的issues版块下的讨论: 亦可见于nim-lang: |
Beta Was this translation helpful? Give feedback.
-
Forward declaration:是在编程语言中提前声明一个函数或类的存在,但不提供其实现。这样可以在函数或类的定义出现之前使用它。 |
Beta Was this translation helpful? Give feedback.
-
以下引自讨论原文
carbon-language/carbon-lang#1416 (comment) I always thought that C and C++ were like this, in order to consume less memory on old machines. Specifically, other languages like Modula-3, which are easier to parse into a complete per-module AST, keep more in memory and can make more passes. i.e. forward declarations let you compile a function at a time, as you encounter them. I don't think it was ever about readability. Originally posted by @jaykrell in carbon-language/carbon-lang#1416 (comment) I think the more full details / rationale and such should be up-coming in #875 which aims to capture this decision in a principle. That said, here is a summary from my memory of the discussion that I ran past the other leads and so I think it is roughly accurate if somewhat brief: The leads felt like the tradeoffs ended up borderline. We specifically walked through the Carbon priorities to look for tradeoffs on each one. Many cases would have a different thing be made easier, but without any clear indication that one was more important than another. For example readability is a priority for Carbon. We know that some readers will prefer a "top-down" structure, but others will prefer to not have to scroll past helper code to find interesting code. It isn't clear that one or the other is significantly more important to optimize for, and neither seemed to be severe problems. Two somewhat minor points were language evolution and migration from C++. The top-down model is expected to be easier to relax into the other if desired compared to the other direction. And the top-down model may minorly reduce the initial surprise of C++ programmers encountering Carbon code as it will follow a fairly similar structure. Or the leads could decide these minor differences aren't big enough to really make a decision, and just let the painter pick a bikeshed color. The painter confirmed the color would be "top down" because it would match the paint of C++. Either way, we end up in the same place. Originally posted by @chandlerc in carbon-language/carbon-lang#472 (comment) |
Beta Was this translation helpful? Give feedback.
-
如何免去前置声明的麻烦: Wikipedia: Multi-pass compilerA multi-pass compiler is a type of compiler that processes the source code or abstract syntax tree of a program several times. This is in contrast to a one-pass compiler, which traverses the program only once. Each pass takes the result of the previous pass as the input, and creates an intermediate output. In this way, the (intermediate) code is improved pass by pass, until the final pass produces the final code. Multi-pass compilers are sometimes called wide compilers,[citation needed] referring to the greater scope of the passes: they can "see" the entire program being compiled, instead of just a small portion of it. The wider scope thus available to these compilers allows better code generation (e.g. smaller code size, faster code) compared to the output of one-pass compilers, at the cost of higher compiler time and memory consumption. In addition, some languages cannot be compiled in a single pass, as a result of their design. Advantages of multi-pass compilersMachine Independent: Since the multiple passes include a modular structure, and the code generation decoupled from the other steps of the compiler, the passes can be reused for different hardware/machines. More Expressive Languages: Multiple passes obviate the need for forward declarations, allowing mutual recursion to be implemented elegantly. The prime examples of languages requiring forward declarations due to the requirement of being compilable in a single pass include C and Pascal, whereas Java does not have forward declarations. |
Beta Was this translation helpful? Give feedback.
-
以下内容来自于Principle: information accumulation: Single-pass "splat" compilation (L58 - L87)In C and other languages of a similar age, single-pass compilation was highly
Global consistencyIn more modern languages such as C#, Rust, Java, and Swift, there is no lexical
Separate declarations and definitions (L122-L127) & (L138-L140)One key benefit of this separation is in reduction of physical dependencies: Another key benefit is that the exported interface of a source file can become |
Beta Was this translation helpful? Give feedback.
-
在过往,前置声明算是出于对资源和性能考量。 |
Beta Was this translation helpful? Give feedback.
-
C++ 是混合的规则。 出自于Principle: information accumulation第89至106行。 The C++ compromiseIn C++, a hybrid approach is taken. There is a C-like lexical information
|
Beta Was this translation helpful? Give feedback.
-
目前般若的类成员函数必须在它前面的才能引用, 因为这和我们的前置声明有一定关系. 如果不支持前置声明, 要实现成员函数的相互引用是比较困难的. |
Beta Was this translation helpful? Give feedback.
-
后面可以持续关注, 目前来说至少 "前置声明优先级是不高的", 考虑到实现难度, 就先不支持了. |
Beta Was this translation helpful? Give feedback.
-
我们围绕函数来讨论, 首先先描述下什么是前置声明,
func test(); 类似于C++, 函数只有声明, 没有实现我们称之为前置声明.
现在我们支持函数的递归.
是否还存在必须需要前置申明才能处理的逻辑. 没有前置申明我们支持不了下面的形式
上述的形式是必须的吗? 函数前置声明还能带来其他好处吗?
Beta Was this translation helpful? Give feedback.
All reactions