I was going through this code (line 41):
https://github.com/black-sat/black/blob/master/src/lib/include/black/logic/parser.hpp
and came across something like this:
#include <iostream>
#define YES
class YES myClass{};
int main(){
cout << "Hi\n";
return 0;
}
What is the purpose of defining a macro and using it in front of a class identifier?
CodePudding user response:
The way you've written it there isn't much point. But if you look at the project's common.hpp file to see how it's used, it makes a lot of sense, and is a common pattern in C and C :
#ifdef _MSC_VER
#define BLACK_EXPORT __declspec(dllexport)
#else
#define BLACK_EXPORT
#endif
...
class BLACK_EXPORT parser
{
public:
...
Here the author has defined compiler-dependent attributes. If the code is built using MSVC, then any class marked with the BLACK_EXPORT macro will get the dllexport
attribute. For other compilers it will do nothing.
CodePudding user response:
If you see, for example:
https://en.cppreference.com/w/cpp/language/class
you'll see that there are a lot of modifiers for a class (for instance, look into the attr optional parameter here: https://en.cppreference.com/w/cpp/language/attributes).
Defining that macro with the required values can adapt the class as required whenever it is included.