C/C++函数/方法装饰
免责声明:我已经有一段时间没有使用 C++ 了……
现在为了提高可读性而装饰 C/C++ 函数/方法声明是否很常见?
粗略示例:
void some_function(IN int param1, OUT char **param2);
以及宏 IN 和 OUT 用空主体定义(即轻量级文档,如果您愿意在本例中使用)。当然,我知道这与与方法/函数关联的“文档注释块”有些并行。
您能否提供一些其他示例...假设该主题对社区有用。 请记住,上面的示例就是这样。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我不会欣赏这样的装饰。
使用 const 和引用以及常量引用会更好,就像
通常 int 是按值传递而不是按引用传递一样,所以我使用 AClass 和 AnotherClass 作为示例。
在我看来,添加 empy IN 和 OUT 会分散注意力。
I wouldn't appreciate such decoration.
Much better to use const and references and constant references, like in
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.
Windows 标头实际上就是这样做的。有关完整列表,请参阅标头注释使用的注释。例如“
对于这个函数,
hModule
是一个可选的输入参数,lpFilename
是一个输出参数,它最多存储nSize
个字符元素,并且返回时会包含(函数的返回值)+1 个字符元素,nSize
为输入参数。Windows headers actually do exactly this. See Header Annotations for the full list of annotations used. For example"
For this function,
hModule
is an optional input parameter,lpFilename
is an output parameter which store a maximum ofnSize
character elements and which will contain (the return value of the function)+1 character elements in it upon return, andnSize
is an input parameter.出于文档目的,编写良好的注释块就足够了,因此这些没有任何作用。此外,一些文档注释解析器有专门的语法来处理这样的事情;例如,给定 Doxygen,您可以编写:
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:
我认为这是一个坏主意。特别是因为任何人都可以定义宏 IN/OUT 并给您带来很多大麻烦。
如果您确实想记录它,请在其中添加注释。
另外,当返回值可以正常工作时为什么要使用 out。
另外,我更喜欢使用 pass by ref 和 const ref 来表明我的意图。此外,当您的代码 const 正确时,编译器现在可以对意图进行相对较好的优化。
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.
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.
不是在 C++ 中,我没有专业地进行过 C 编程,但至少在 C++ 中,参数的类型是不言自明的:
与代码内文档工具(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:
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)...
我尝试使用:
大多数时候很容易看出哪些是 IN 或 OUT 参数,当然还有声明中的正确名称是一个很好的文档。
我发现那些 IN、OUT 插件很烦人。
I try to use:
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.
我见过这个,但我不认为我会说这是“常见”。
Win32 API(C 而非 C++)使用类似的东西:
对于 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:
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.唯一比这更糟糕的是很久以前在 Pascal dev 编写的 C 程序中看到的:
The only thing worse then this was seen long ago in a C program written by Pascal dev:
我以前没见过这个。我认为将这样的信息放在评论中会更好。
I have not seen this before. I would think it would be better to put information like this in comments.
除了参数类型中的信息之外,我还看到了前缀 i_、o_、io_ 的使用:
I saw usage of prefixes i_, o_, io_ in addition to information in parameter types: