内联函数的前向声明

发布于 2025-01-05 22:57:12 字数 281 浏览 3 评论 0原文

我有一个头文件,其中将包含大量(30+)内联函数。

我不想让读者滚动或搜索内联函数的定义(实现),而是希望有一个前向声明部分来声明函数声明以及描述该函数的注释。本节将允许读者了解如何使用函数或查找函数,而无需向下滚动到实现。

另外,我希望读者养成使用函数的习惯,而无需查看其实现。

独立函数的前向声明的语法是什么?

{这适用于 C99 和 C++}

仅供参考,我正在使用 IAR Workbench C 编译器设置为使用 C99。

I have a header file that is going to contain a large amount (30+) of inline functions.

Rather than having the reader scroll or search for the definition (implementation) of the inline function, I would like to have a forward declaration section that states the function declaration with comments describing the function. This section would allow the reader to find out how to use a function or to look for a function without having to scroll down to the implementation.

Also, I would like the readers to get in the habit of using functions without having to see their implementations.

What is the syntax for a forward declaration of a stand-alone function?

{This applies to C99 and C++}

FYI, I am using IAR Workbench C compiler set to use C99.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南街九尾狐 2025-01-12 22:57:12

与非内联函数没有什么不同:

void func();       // "forward" declaration

// ...

inline void func() // definition
{
    // impl
}

通常,用于向库使用者“隐藏”定义的模式是将声明放在一个标头 (ah) 中,并将定义放在第二个标头 (< code>a_def.h),然后让前者 #include 后者(为简洁起见,省略了包含防护):

// a.h
void func();
#include "a_def.h"
// a_def.h
inline void func()
{
    // impl
}

库使用者只需 #include.

No differently than a non-inline function:

void func();       // "forward" declaration

// ...

inline void func() // definition
{
    // impl
}

Typically the pattern used to "hide" the definitions from the library consumer is to put the declarations in one header (a.h) and the definitions in a second header (a_def.h), then have the former #include the latter (inclusion guards omitted for brevity):

// a.h
void func();
#include "a_def.h"
// a_def.h
inline void func()
{
    // impl
}

The library consumer would simply #include <a.h>.

眼前雾蒙蒙 2025-01-12 22:57:12

您不需要“转发声明”它(该术语通常仅适用于类型,因为我们通常在首先声明它们的同一位置定义它们)。

只需像平常一样声明它即可:

#include <iostream>

void foo();            // Declaration

inline void foo() {    // Defining declaration
   std::cout << "!";
}

// ---------------------------------------------

int main() {
   foo();              // Output: foo()
}

现场演示。

You don't need to "forward declare" it (a term that is usually only applied to types, since we usually define them in the same place that we first declare them).

Just declare it, like normal:

#include <iostream>

void foo();            // Declaration

inline void foo() {    // Defining declaration
   std::cout << "!";
}

// ---------------------------------------------

int main() {
   foo();              // Output: foo()
}

Live demo.

淡写薰衣草的香 2025-01-12 22:57:12

要同时使用 C++ 和 C99,请将声明也设为内联,如下所示:

// declaration
inline void foo(void);

// implementation 
inline void foo(void) {
    print("foo");
}

To work with both C++ and C99, make the declaration inline too, like this:

// declaration
inline void foo(void);

// implementation 
inline void foo(void) {
    print("foo");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文