Name of C/C++ stdlib naming convention?

倾然丶 夕夏残阳落幕 提交于 2019-12-09 07:34:54

问题


I wonder if the naming convention used in C/C++ standard libraries has a name, or at least a cheat-sheet where I can lookup the rules. E.g.

push_back    -- underscore used
setstate     -- but not used here!
string::npos -- when to use abbreviations?
fprintf
...

Does the naming convention used in the C/C++ standard libraries have a specific name?


回答1:


C/C++ uses the famous make-stuff-up-as-we-go-along naming convention. In general, the only consistent things you can say about the C/C++ standard library naming convention is that it doesn't use camel case (except for C++ STL template typenames), it uses lower-case class and function names, and (sometimes) uses underscores to separate words. But the usage of underscores is more of a C++ thing; C in particular tends to abbreviate words, e.g. strlen, memcpy. This is probably a result of the tendency to use very terse names for things in the early days of UNIX.




回答2:


The C standard library has well defined rules you must follow to avoid name conficts. I don't know anything about C++ though.

If you think this is a mess you should check out the PHP library...




回答3:


I think there are several groups invented at different times by different people and using somewhat different conventions: C libraries, streams, strings, STL (containers + algorithms + iterators). I have a feeling that the latter might be seen as the convention, which sets the example for things like boost and C++0x naming.




回答4:


The sad truth is - there is no real convention.

See the argument order of stdio functions as a painful reminder.




回答5:


I don't have the answer, but at least, I don't think that C and C++ use the same naming convention.




回答6:


I don't think there is a name for either set of naming conventions.

The naming conventions in the C and C++ standards are different, though faintly related.

In particular, in the C library, no function or macro name contains an underscore (AFAICR), unlike the C++ library.

Most of the C library naming convention was controlled by precedent; it standardized existing practice (as of about 1984), with remarkably few inventions (locale handling via <locale.h> being the main one).

The C++ library was based on precedent too - but a different set of precedents, and I think it is arguable that the STL was not in as widespread use prior to its adoption by the standard as the functions in the C library were.



来源:https://stackoverflow.com/questions/1734277/name-of-c-c-stdlib-naming-convention

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!