Why don't win32 API functions have overloads and instead use Ex as a suffix?

The win32 API, for example, has two methods: StrFormatByteSize and StrFormatByteSizeEx. Despite the fact that both methods pretty well do the same thing, and part of the Ex counter offers only a new parameter to slightly change the behavior, then could they have two overloads of the same function?

Is this a limitation of c / C ++ or what is the possible reason for such an inconvenient agreement?

+4
source share
4 answers

The Win32 API is a C (not C ++) API. C language does not support overloaded functions.

Aside: the Win32 API uses __stdcall -decorated functions that include the number of parameter bytes as part of the function name. __stdcall not part of the C language, but Windows linkers should be aware of this.

Microsoft could use this to implement some kind of overload, but (because many languages ​​do not understand overload), which would limit the number of languages ​​that could be used to program Windows.

+26
source

C does not support function overloading at all.

+11
source

Is this a limitation of c / C ++ or what is the possible reason for such an inconvenient agreement?

Yes, and the reason C does not support overload functions is because the name mangling (the function name translation to be used by the linker) used for standard C does not take into account its functional parameters.
That is, void func(int) in C will work before _func so you cannot have func(int) and func(bool) together, since both will be converted to _func .

While in C ++, a garbled name for a function takes into account all of its functional parameters, but since C ++ name management has not been standardized, changing the name depends on the compiler.

Another thing to keep in mind is not to consider the return parameter of the function in the rooted name. Therefore, you cannot have overloaded functions like void func(int) and bool func(int) together.

- Samrat Patil

+2
source

Microsoft never bothered.

Of course, several people say that C does not support overloading. It does not matter. The API already uses overload, C-style. For example, the StrFormatByteSize function that you mentioned does have two overloads: LPSTR StrFormatByteSizeA(DWORD dw, LPSTR pszBuf, UINT cchBuf) and LPWSTR StrFormatByteSizeW( LONGLONG qdw, LPWSTR pszBuf, UINT cchBuf); . The problem with this mechanism, of course, is that it does not generalize well in various _Ex suffixes.

Microsoft could add a header that provides StrFormatByteSize as two built-in C ++ functions, not C macros. If they did, it would be easy to add a third overload for the _Ex suffix. There is no such C ++ header, and therefore, no C ++ overloads exist at all.

0
source

All Articles