Is there a __CLASS__ macro in C++?

The problem with using typeid(*this).name() is that there is no this pointer in a static method call. The macro __PRETTY_FUNCTION__ reports a class name in static functions as well as method calls. However, this will only work with gcc. Here’s an example of extracting the information through a macro style interface. inline std::string methodName(const std::string& … Read more

Inline functions vs Preprocessor macros

Preprocessor macros are just substitution patterns applied to your code. They can be used almost anywhere in your code because they are replaced with their expansions before any compilation starts. Inline functions are actual functions whose body is directly injected into their call site. They can only be used where a function call is appropriate. … Read more

Constexpr vs macros

Aren’t they basically the same? No. Absolutely not. Not even close. Apart from the fact your macro is an int and your constexpr unsigned is an unsigned, there are important differences and macros only have one advantage. Scope A macro is defined by the preprocessor and is simply substituted into the code every time it … Read more

How to pass macro definition from “make” command line arguments (-D) to C source code?

Call make command this way: make CFLAGS=-Dvar=42 And be sure to use $(CFLAGS) in your compile command in the Makefile. As @jørgensen mentioned , putting the variable assignment after the make command will override the CFLAGS value already defined the Makefile. Alternatively you could set -Dvar=42 in another variable than CFLAGS and then reuse this … Read more

What’s the use of do while(0) when we define a macro? [duplicate]

You can follow it with a semicolon and make it look and act more like a function. It also works with if/else clauses properly then. Without the while(0), your code above would not work with if (doit) INIT_LIST_HEAD(x); else displayError(x); since the semicolon after the macro would “eat” the else clause, and the above wouldn’t … Read more

Where are the recorded macros stored in Notepad++?

In Windows the macros are saved at %AppData%\Notepad++\shortcuts.xml (Windows logo key + E and copy&paste %AppData%\Notepad++\) Or: In Windows < 7 (including Win2008/R2) the macros are saved at C:\Documents and Settings\%username%\Application Data\Notepad++\shortcuts.xml In Windows 7|8|10 C:\Users\%username%\AppData\Roaming\Notepad++\shortcuts.xml Note: You will need to close Notepad++ if you have any new macros you want to ‘export’. Here is … Read more

What does the Q_OBJECT macro do? Why do all Qt objects need this macro?

From the Qt documentation: The Meta-Object Compiler, moc, is the program that handles Qt’s C++ extensions. The moc tool reads a C++ header file. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces a C++ source file containing the meta-object code for those classes. Among other things, meta-object code … Read more

What is the worst real-world macros/pre-processor abuse you’ve ever come across?

From memory, it looked something like this: #define RETURN(result) return (result);} int myfunction1(args) { int x = 0; // do something RETURN(x) int myfunction2(args) { int y = 0; // do something RETURN(y) int myfunction3(args) { int z = 0; // do something RETURN(z) Yes that’s right, no closing braces in any of the functions. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)