5

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?

1
  • 6
    C/C++ is not a language. The Windows API is a C API.
    – Thomas
    Dec 1, 2009 at 10:04

4 Answers 4

30

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.

2
  • 1
    __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
    Nov 30, 2009 at 9:31
  • 1
    @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. Nov 30, 2009 at 9:41
12

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

2

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

1
  • 2
    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. Nov 30, 2009 at 14:55
0

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.

3
  • 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. Dec 1, 2009 at 10:11
  • It actually doesn't need support/documentation since this would be an automatic transformation.
    – MSalters
    Dec 1, 2009 at 14:02
  • If it exists, it needs support and documentation. Dec 9, 2009 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.