7

Both the C++ standard library implementation that comes with Visual Studio and the windows API (both Win32 and MFC) includes loads of macros.

For examples they have defined "TRUE" and "FALSE" as 1 and 0. And replaces bool in all contexts with a "BOOL" which is a typedef to an int. And they are not even using three way logic.

There is also the "min" and "max" macros, which could easily be replaced with templates.

There are also tons of defines that are simply defined to nothing such as "far" and "near".

Braiam
  • 1
  • 11
  • 47
  • 78
David
  • 818
  • 13
  • 30
  • 15
    Probably because it's leftover from C code, where those macros were sensible at the time. – Pubby Mar 20 '13 at 18:34
  • 1
    Because it's C, not C++. – Ed S. Mar 20 '13 at 18:36
  • 1
    @Pubby Makes sense for the Windows API, but not for (large parts of) the C++ standard library implementation. –  Mar 20 '13 at 18:36
  • 12
    *"There are also tons of defines that are simply defined to nothing such as "far" and "near"."* - You need to brush up on your history. Computers were not born in the era of protected mode 32-bit CPU's, and you don't just haul off and break working programs to clean up your API (well, not if you want to have a loyal developer base anyway). – Ed S. Mar 20 '13 at 18:37
  • 6
    None of the things you noted are in the C++ library. They are all in the Windows headers, which as others noted, dates back to the early 1980's. – Raymond Chen Mar 20 '13 at 18:45
  • And as far as the standard library is concerned, I'm not sure what your definition of "loads of macros" is, but from my purview, it certainly doesn't qualify as "loads". There's a few here and there, but most of them are to do with conditional compilation, which isn't well handled without them. – Benjamin Lindley Mar 20 '13 at 18:47
  • Okay then here is an example: to_string(). Implemented in visual studio 11. A part of the new c++ 11 "standard". Does a direct call to the macro _TOSTRING. – David Mar 20 '13 at 18:47

2 Answers2

13

Much of this code began life as C code, and dated from the 1980s. If it were being written from scratch today then you could expect the code to look a little different.

For example, you pick out the BOOL type. Back in the day, when the Windows API was first conceived, there was no boolean type in C.

You mention the MIN and MAX macros and propose templates. Well, there are no templates in C as you know and those macros probably even pre-date C++ templates.

Although MFC is a C++ library, it stands atop Win32 which is a C API. And so the implementation of MFC will clearly have to use that C API in its implementation.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • There's still no boolean type in most modern C code - because barely anybody uses `_Bool` or `stdbool.h` from C99. – Charles Salvia Mar 20 '13 at 18:37
  • Ok but the c++ standard library cant possibly have been written before c++? – David Mar 20 '13 at 18:38
  • 3
    @DavidJensen: What does the C++ standard library have to do with a C API? Your premise is wrong; the Windows API is a C, not C++, API. You may as well be asking why the Windows API doesn't use C# generics. – Ed S. Mar 20 '13 at 18:38
  • @EdS. the question mentions that library. – Drew Dormann Mar 20 '13 at 18:40
8

In Ye Olden C days (MS-DOS), far and near were pointer keywords. Many of these libraries were written in C to start with: therefore, instead of re-inventing the whole standard library from scratch, they just used something that already worked, and already worked reasonably well.

Also, Windows is mostly in essence a C API, not a C++ one, so you can expect many things (like Macros, strange definitions, etc.) to be present in the code.

  • Re `near` and `far`: Have you seen this: http://stackoverflow.com/q/3869830/140719? – sbi Mar 29 '13 at 23:05