lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It's a delicate balance. Macros were often necessary in C, because it
didn't have inlining and you could get a serious performance increase.
But C macros are not very intelligent, and were applied by people with
'arbitrary tastes'.  So C++ people regard them as the work of the
devil. Lua is naturally fully polymorphic so a lot of the traditional
uses of macros aren't necessary.

That's correct. However, there are still some cases when macros cannot be avoided even in modern C++, and C++ programmers are not afraid to use macros in such cases.
Looking at my C++ code I see that I use macros for the following:
(that's C++98, some of these may become obsolete in C++09)

1. Static (compile-time) asserts

2. Functions which need to be evaluated at compile-time (for example, calculating the size of an array can be done with a template function, but in C++98 such function cannot be used to define the size of an array)

3. Issuing diagnostic messages containing location (file/line number) info

4. Executing different code in different configurations of the code, most often Debug vs. Release, with the most notable example being the ASSERT macro. The reasons to do this are mostly two: optimization (for the Release build) and providing additional info (for the Debug build).

5. Supporting different compilers

6. Defining constants to be used in Windows resource files (.rc)

7. Rare cases to avoid code duplication where templates cannot do the job. For example, providing boilerplate code for different needs, most often system needs - e.g., declaring a module's interface in different formats, or defining DLLMain and a factory function for a dynamically loaded plugin. This case is closely related to the next one:

8. Creating new syntax where the language doesn't have the needed facilities. Templates and built-in operator overloading are powerful C++ facilities for syntax modification, but for example it is impossible to come up with a nice syntax to define unit tests without resorting to macros.

As you can see, half of these cases are not relevant to Lua - 1, 3, 5, 6. I'm not sure about the rest of the cases, so far I haven't accumulated enough Lua code and experience to see such needs, but maybe others have seen them.

Regards,
Ivan