how to save typing in c++ - c++

I have a piece of code like this I need to put in front of a lot of functions.
#ifdef __AA__
__BB__ __CC__
#endif
But it's just too trivial to write it every time and it makes code looks ugly.
Is there any way I can define something like macro to make it short? Thanks.

You can define a preprocessor macro conditionally:
#ifdef __AA__
# define FN_INTRO __BB__ __CC__
#else
# define FN_INTRO
#endif
Then before each function you can just write FN_INTRO instead of the whole thing.

Related

What does this C++ define macro do?

What does this define macro do? I assumed that this will print the given string to the standard output, but it just not printed out nothing. Am I wrong about this?
#define SCOPE_LOGGER(...)
...
void someClass::someFunction() { SCOPE_LOGGER("someClass::someFunction()"); ... }
As per KamilCuk's answer, that macro expands to nothing.
However, most likely it does expand to something through some other preprocessing conditional path, i.e. imho that line appears in the code like this:
#ifdef DEBUG
#define SCOPE_LOGGER(...) something real that does the logging
#else
#define SCOPE_LOGGER(...)
#endif
That's the only way that line can make sense.
What does this define macro do?
Defines a macro function that takes any number of arguments and expands to nothing.
Am I wrong about this?
Because the macro expands to nothing, SCOPE_LOGGER("someClass::someFunction()") is removed from the code. Fun fact: the trailing ; stays.

Define macros with condition in C++

I found some existing answer on SO, but I actually don't understand how does everything there works.
Basically, I want to define a macro in if/switch condition
if (condition)
{
#define SOME_MACRO
}
So, if some condition is true, there will be defined some new macro.
But, as for now, it just defines macro anyway. I understand that it's not really how it should be done, but there's no normal explanation to that.
You can't mix macros and code like that. Macros are all processed before your code is compiled, it is just a convenience feature to save typing (and make code easier to read).
There is a macro syntax for conditions for example, you can do:
#if Condition
#define X
#endif
to conditionally define a macro.

Avoid macro to be usable from client code

I'm currently working on a header only library and I'd like to be able to use some macros without them being usable from client code. Example:
// library_header1.h
#define MACRO_NUMBER_1(__X__) doSomethingWith(__X__) // etc...
class LibraryClass1
{
// We'll use the macro somewhere in here.
};
Now, if I include library_header1.h i'm able to use MACRO_NUMBER_1. Is there any way I can avoid that?
Others already mentioned #undef but what if you have many macros you need to undefine and you need to use them in many headers? This situation can be dealt with by having a header define and undefine macros and bewteen that include a specified header. For example:
// temp-macros.h
#if defined(OUTER)
# define MACRO x
# include OUTER
# undef MACRO
# undef OUTER
#endif
The idea is to set up OUTER from the header where the macros are neede, include temp-macros.h and use the macros in the second inclusion:
// macro-use.h
#if !defined(OUTER)
# define OUTER "macro-use.h"
# include "temp-macros.h"
#elif !defined(INCLUDED_MACRO_USE)
# define INCLUDED_MACRO_USE
void use(int MACRO) { /*...*/ }
#endif
Of course, this example uses just one trivial macro but if there are more macros and they are more complicated this approach could be useful.
To prevent the library user from accessing the macro - you can put #undef when you know you won't need your macro anymore.
If you want to prevent intellisense/autocompletion from ever seeing your macro, you may try to trick it by skipping it over the macro definition. Usually you can do it as follows:
set up your compilation project (visual solution, makefile, ...) so that you pass an extra "identification" macro. Something like -DIAMCOMPILING
Guard your macro definition with #ifdef IAMCOMPILING ... #endif
your IDE will most likely not recognise IAMCOMPILING and skip over the definition of your macro.
Do note however, that then IDE will get confused when your actually use the macro in the header file and some nonexistent errors will be flagged.
Whilst it won't stop the macro from being noticed by the IDE, if you use a unusual prefix to the macro, it will be less likely to collide with something that the user will want to do, and thus be less offensive. It may not be the ideal solution, but prefixing the macro with CCC or XWX or something else that is unlikely to be used by the normal programmer may help.
(Or, don't use macros is always a solution that works!)

Can macros in C++ define macros?

I was wondering if it was possible to define a macro in C++ that defines another macro that can be used in later code. Is this possible, or is the preprocessor used by g++ too limited for this?
No, you cannot define a macro within the expansion of another macro.
Nope, you can't define a macro as a macro.
The preprocessor makes only one pass over the source code, so this is not possible. However, you could use an external tool to perform some preprocessing ahead of compilation, like m4.
You can do something like this, its not exactly what you are looking for, but it might help.
#ifdef ENABLE_MACRO_1
#define PRINT_MACRO(varName) \
std::cout<<varName<<std::endl;
#else
#define PRINT_MACRO(varName) \
//do nothing
#endif
So you can define a macro depending on another preprecursor condition which was defined defined.

Preprocessor macros: how to insert arguments?

The code has a number of following sections:
int filter;
#ifdef INPUTFILTER_FOO
LOG4CXX_DEBUG(log, "FOO filter used");
filter = F_FOO;
#endif
They are used multiple times in the code (used to provide I/O, threading support etc for all testing configurations), Circa they are essential for debugging but make the code look harsh, want to replace them with macros, one for each category_type namespace.
So, want to expand the following:
MACROSTUFFBAZ(log2, stuff, "BAZ") <- the text part is unique for each class, so it needs to be included in macro too.
to:
#ifdef INPUTSTUFF_BAZ
LOG4CXX_DEBUG(log2, "BAZ stuff used");
stuff = S_BAZ;
#endif
To define macros, plan to use this:
debug.hpp:
#ifdef INPUTSTUFF_BAZ
#define MACROSTUFFBAZ ...
#else
#define MACROSTUFFBAZ
.. no code!
#endif
#endif
(at least this will give a clear overview of the things currently undergoing probation, without seeing them around the code)
Here's my try, although i'm not 100% sure if it works cause i'm unable to test it right now, and also different compilers handle preprocessor macros a little bit differently. But I think something like this works at least in visual studio.
Basically, you have to use a helper macro to convert the parameter into a string. And secondly, you can use ## to concatenate identifiers:
#define PRMTOSTR_HLPR(x) #x
#define PRMTOSTR(x) PRMTOSTR_HLPR(x)
#ifdef INPUTSTUFF_BAZ
#define MACROSTUFFBAZ(A,B,C) \
LOG4CXX_DEBUG(A, PRMTOSTR(C)" stuff used"); \
B = S_##C;
#else
#define MACROSTUFFBAZ(A,B,C)
#endif
//used like this:
MACROSTUFFBAZ(log2, stuff, BAZ)
edit: The helper macro is actually not needed here, so you can just put #C directly in the definition of MACROSTUFFBAZ.