views:

377

answers:

4

The win32 API has for example two methods StrFormatByteSize and StrFormatByteSizeEx. Even though both the methods symantically do the same thing and the Ex counter part only offers a new parameter to slightly change the behavior then couldn't they have two overloads of the same function?

Is it a limitation of c/c++ or what is the possible reason for such an awkward convention?

+23  A: 

The Win32 API is a C (not C++) API. The C language doesn't support overloaded functions.

Complete aside: The Win32 API uses __stdcall-decorated functions, which include the number of bytes of parameters as part of the function name. __stdcall is not part of the C language, but Windows linkers have to know about it.

Microsoft could have used this to implement some kind of overloading, but (since a lot of languages don't understand overloading) that would limit the number of languages that could be used to program Windows.

Roger Lipscombe
__stdcall is not part of the C language. The C language doesn't support overloaded functions, period. Your answer would be better and more correct without the second paragraph.
jalf
@jalf: True, but since \_\_stdcall is part of the Win32 API, you _could_ use it for limited overloading. It would be a really bad idea. I've expanded my original aside to clarify that.
Roger Lipscombe
+10  A: 

The C language doesn't support function overloading at all.

Greg Hewgill
+2  A: 

Is it a limitation of c/c++ or what is the possible reason for such an awkward convention?

Yes, and the reason that C doesn't support overloading functions is because, name mangling(conversion of function names to be used by the linker) used for standard C doesn't account for its function parameters.
I.e. void func(int) in C gets mangled to _func so you cannot have func(int) and func(bool) together, as both will be converted to _func.

Whereas in C++, the mangled name for a function accounts for all its function parameters, but as name mangling in C++ was not standardized, the name mangling is compiler dependent.

One more thing to keep in mind is C++ doesn't consider the return parameter of the function in the mangled name. Hence, one cannot have overloaded functions as void func(int) and bool func(int) together.

--Samrat Patil

Samrat Patil
I'd say it's the other way around - the reason name mangling doesn't account (and doesn't have to account) for function parameters is that C doesn't support overloading. In 1980, the C language was a consequence of its implementation, but in 2009 C implementations are a consequence of the language standard. Name mangling schemes are not part of the standard, and for example MSVC mangles `void func(int)` to `_func` for cdecl, `_func@4` for stdcall and `@func@4` for fastcall.
Steve Jessop
A: 

Microsoft never bothered.

Sure, a few people here say that C doesn't support overloading. That's irrelevant. The API already uses overloading, C-style. For instance, the StrFormatByteSize function you mentioned really has two overloads: LPSTR StrFormatByteSizeA(DWORD dw, LPSTR pszBuf, UINT cchBuf) and LPWSTR StrFormatByteSizeW( LONGLONG qdw, LPWSTR pszBuf, UINT cchBuf);. The problem with this mechanism is of course that it generalizes poorly to the various _Ex suffixes.

Microsoft could have added a header which provides StrFormatByteSize as two inlined C++ functions, instead of C macros. Had they done so, it would have been easy to add the third overload for the _Ex suffix. There's no such C++ header, though, and therefore no C++ overloads at all.

MSalters
But why should Microsoft provide this kind of thing and then have to support and document it when the Win32 API is a C API and everyone can quite happily use it as such? I always wrap third party API usage with my own C++ code that deals with parameter sanity and throws exceptions on failure, etc, but my view of what's right and proper in these situations is probably very different to yours and very different to the guy at MS who would have to write these shims. Since I'd probably still wrap their official C++ shims I'd rather they expended their energy with more useful tasks.
Len Holgate
It actually doesn't need support/documentation since this would be an automatic transformation.
MSalters
If it exists, it needs support and documentation.
Roger Lipscombe