C/C++函数/方法装饰

发布于 2024-08-09 13:26:50 字数 352 浏览 2 评论 0原文

免责声明:我已经有一段时间没有使用 C++ 了……

现在为了提高可读性而装饰 C/C++ 函数/方法声明是否很常见?

粗略示例:

void some_function(IN int param1, OUT char **param2);

以及宏 INOUT主体定义(即轻量级文档,如果您愿意在本例中使用)。当然,我知道这与与方法/函数关联的“文档注释块”有些并行。

您能否提供一些其他示例...假设该主题对社区有用。 记住,上面的示例就是这样。

DISCLAIMER: I haven't done C++ for some time...

Is it common nowadays to decorate C/C++ function/method declarations in order to improve readability?

Crude Example:

void some_function(IN int param1, OUT char **param2);

with the macros IN and OUT defined with an empty body (i.e. lightweight documentation if you will in this example). Of course I understand this goes somewhat in parallel with the "doc comment block" associated with the method/function.

Could you provide some other examples... assuming this topic is useful to the community. Please bear in mind that the example above is just what it is.

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

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

发布评论

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

评论(10

迷荒 2024-08-16 13:26:50

我不会欣赏这样的装饰。

使用 const 和引用以及常量引用会更好,就像

void some_function(AClass const ¶m1, AnotherClass ¶m2)

通常 int 是按值传递而不是按引用传递一样,所以我使用 AClass 和 AnotherClass 作为示例。
在我看来,添加 empy IN 和 OUT 会分散注意力。

I wouldn't appreciate such decoration.

Much better to use const and references and constant references, like in

void some_function(AClass const ¶m1, AnotherClass ¶m2)

Usually int are passed by value and not by reference, so I used AClass and AnotherClass for the example.
It seems to me that adding empy IN and OUT would be distracting.

山川志 2024-08-16 13:26:50

Windows 标头实际上就是这样做的。有关完整列表,请参阅标头注释使用的注释。例如“

DWORD
WINAPI
GetModuleFileName(
    __in_opt HMODULE hModule,
    __out_ecount_part(nSize, return + 1) LPTSTR lpFilename,
    __in DWORD nSize
    );

对于这个函数,hModule是一个可选的输入参数,lpFilename是一个输出参数,它最多存储nSize个字符元素,并且返回时会包含(函数的返回值)+1 个字符元素,nSize 为输入参数。

Windows headers actually do exactly this. See Header Annotations for the full list of annotations used. For example"

DWORD
WINAPI
GetModuleFileName(
    __in_opt HMODULE hModule,
    __out_ecount_part(nSize, return + 1) LPTSTR lpFilename,
    __in DWORD nSize
    );

For this function, hModule is an optional input parameter, lpFilename is an output parameter which store a maximum of nSize character elements and which will contain (the return value of the function)+1 character elements in it upon return, and nSize is an input parameter.

汐鸠 2024-08-16 13:26:50

出于文档目的,编写良好的注释块就足够了,因此这些没有任何作用。此外,一些文档注释解析器有专门的语法来处理这样的事情;例如,给定 Doxygen,您可以编写:

/**
 * @param[in]  param1 ...
 * @param[out] param2 ...
 **/
void some_function(int param1, char **param2);

For documentation purposes, a well-written comment block is sufficient, so these don't serve any purpose. Furthermore, some documentation comment parsers have special syntax for just such a thing; for example, given Doxygen, you could write:

/**
 * @param[in]  param1 ...
 * @param[out] param2 ...
 **/
void some_function(int param1, char **param2);
半岛未凉 2024-08-16 13:26:50

我认为这是一个坏主意。特别是因为任何人都可以定义宏 IN/OUT 并给您带来很多大麻烦。

如果您确实想记录它,请在其中添加注释。

void some_function(/* IN */ int param1, /* OUT */ char **param2);

另外,当返回值可以正常工作时为什么要使用 out。
另外,我更喜欢使用 pass by ref 和 const ref 来表明我的意图。此外,当您的代码 const 正确时,编译器现在可以对意图进行相对较好的优化。

void some_function(/* IN */ int const& param1, /* OUT */ char*& param2);
// OK for int const& is kind of silly but other types may be usefull.

I think this is a bad idea. Especially since anybody can come along and define the macros IN/OUT and leave you in heap big trouble.

If you really want to document it put comments in there.

void some_function(/* IN */ int param1, /* OUT */ char **param2);

Also why use an out when a return value will work fine.
Also I would prefer to use pass by ref and const ref to indicate my intentions. Also the compiler now does relatively good optimsing for intent when your code is const correct.

void some_function(/* IN */ int const& param1, /* OUT */ char*& param2);
// OK for int const& is kind of silly but other types may be usefull.
葮薆情 2024-08-16 13:26:50

不是在 C++ 中,我没有专业地进行过 C 编程,但至少在 C++ 中,参数的类型是不言自明的:

void f( std::string const & ); // input parameter
void f( std::string );         // input parameter again (by value)
void f( std::string& );        // in/out parameter
std::string f();               // output

与代码内文档工具(doxygen)一起,您可以在参数中添加一些上下文(期望什么值)或者函数不可接受,函数如何更改传入的对象...

关于指针:我们倾向于限制方法接口中的原始指针,当需要时,可以使用它们,但通常应该首选智能指针。再说一遍,所有权语义来自于智能指针的选择:shared_ptr<>用于稀释共享责任(或在需要时),auto_ptr<>/unique_ptr<>用于单一所有权(通常作为来自工厂、本地或成员的返回值)。属性)...

Not in C++, I have not done C programming professionally but at least in C++ the type of the parameters is self-explanatory:

void f( std::string const & ); // input parameter
void f( std::string );         // input parameter again (by value)
void f( std::string& );        // in/out parameter
std::string f();               // output

That together with in-code documenting tools (doxygen) where you add some context to the parameters (what values are expected or unacceptable by the function, how the function does change the passed in objects...

About pointers: We tend to limit raw pointers in our method interfaces. When need be, they can be used, but in general smart pointers should be preferred. Then again, ownership semantics come from the choice of smart pointer: shared_ptr<> for diluted shared responsibility (or when needed), auto_ptr<>/unique_ptr<> for single ownership (usually as return value from factories, locals or member attributes)...

成熟的代价 2024-08-16 13:26:50

我尝试使用:

  • 输入参数或引用的值(如果它们很大)
  • 输出参数的引用
  • 赋予被调用函数所有权的指针

大多数时候很容易看出哪些是 IN 或 OUT 参数,当然还有声明中的正确名称是一个很好的文档。

我发现那些 IN、OUT 插件很烦人。

I try to use:

  • Values for input parameters or references if they are big
  • References for out parameters
  • Pointers to give ownership to the called function

Most of the time is really easy to see which are IN or OUT parameters, of course proper names in the declaration are a good documentation.

I find those IN, OUT addons annoying.

无人接听 2024-08-16 13:26:50

我见过这个,但我不认为我会说这是“常见”。

Win32 API(C 而非 C++)使用类似的东西:

WINADVAPI
BOOL
WINAPI
CreateProcessWithLogonW(
    __in        LPCWSTR lpUsername,
    __in_opt    LPCWSTR lpDomain,
    __in        LPCWSTR lpPassword,
    __in        DWORD dwLogonFlags,
    __in_opt    LPCWSTR lpApplicationName,
    __inout_opt LPWSTR lpCommandLine,
    __in        DWORD dwCreationFlags,
    __in_opt    LPVOID lpEnvironment,
    __in_opt    LPCWSTR lpCurrentDirectory,
    __in        LPSTARTUPINFOW lpStartupInfo,
    __out       LPPROCESS_INFORMATION lpProcessInformation
      );

对于 Visual C++ 2005 及更高版本的编译器,这些实际上映射到诸如 __$allowed_on_parameter 之类的声明,并在编译时进行检查。

I have seen this, but I don't think I would say it's "common."

The Win32 API (C not C++) uses something similar:

WINADVAPI
BOOL
WINAPI
CreateProcessWithLogonW(
    __in        LPCWSTR lpUsername,
    __in_opt    LPCWSTR lpDomain,
    __in        LPCWSTR lpPassword,
    __in        DWORD dwLogonFlags,
    __in_opt    LPCWSTR lpApplicationName,
    __inout_opt LPWSTR lpCommandLine,
    __in        DWORD dwCreationFlags,
    __in_opt    LPVOID lpEnvironment,
    __in_opt    LPCWSTR lpCurrentDirectory,
    __in        LPSTARTUPINFOW lpStartupInfo,
    __out       LPPROCESS_INFORMATION lpProcessInformation
      );

In the case of the Visual C++ 2005 and later compilers, these actually map to declarations like __$allowed_on_parameter and are checked at compile time.

洋洋洒洒 2024-08-16 13:26:50

唯一比这更糟糕的是很久以前在 Pascal dev 编写的 C 程序中看到的:


#define begin {
#define end   }

int main( int argc, char* argv[] )
begin
  ...
end

The only thing worse then this was seen long ago in a C program written by Pascal dev:


#define begin {
#define end   }

int main( int argc, char* argv[] )
begin
  ...
end
撕心裂肺的伤痛 2024-08-16 13:26:50

我以前没见过这个。我认为将这样的信息放在评论中会更好。

I have not seen this before. I would think it would be better to put information like this in comments.

国产ˉ祖宗 2024-08-16 13:26:50

除了参数类型中的信息之外,我还看到了前缀 i_、o_、io_ 的使用:

void some_function(int i_param1, char** o_param2, int& io_param3);

I saw usage of prefixes i_, o_, io_ in addition to information in parameter types:

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