Is it possible to selectively compile in certain sections of code with templates, or is this limited to the preprocessor? For example, if I wanted to remove a section of code with the preprocessor, I know I can do:
#if 0
static const char[] hello_world = "hello, world";
#endif
Is there anyway to do the same with templates?
Just in case I’m asking the wrong question, here’s what I’m trying to do. I want to load some code on starting the application. Normally, I would just use a constructor to do whatever I wanted and create a static variable. But I want this to only happen in the debug build and the code to not be run during the release build. The code that I am doing this with is created with a macro, so I don’t seem to be able to put “#if 0” inside of a macro and have it expand correctly.
Is there anyway to do this in C++?
EDIT: Here’s an example of the macro code I’m currently using.
#define unittest(NAME) \
struct unittest_ ## NAME : \
public unittest::unittest_template<unittest_ ## NAME> \
{ \
unittest_ ## NAME() : \
unittest::unittest_template<unittest_ ## NAME>(#NAME) {} \
void run_test(); \
}; \
static unittest_ ## NAME NAME ## _unittest; \
void unittest_ ## NAME::run_test()
The code is used by doing:
unittest(addTest)
{
assert_(5, 5); // there's an assert statement in the code
}
I like the syntax of how it looks, but I don’t see a way to get rid of the body of the function using macros. I tried using a begin/end macro instead and got:
#ifdef UNITTEST
# define unittest_begin(NAME) // previous code
# define unittest_end() // nothing needed
#else
# define unittest_begin(NAME) #if 0
# define unittest_end() #endif
#endif
This doesn’t seem to work.
EDIT2: The original question is quite different from what it turned into. Changing the name so hopefully it’s more relevant to the actual question.
Given your edit, it seems like you’re making this a lot harder than it has to be. Where you define your macro, provide an
#ifdefblock there, and chose how you define it.You could also use a single definition there, and change
maintoA final option would be to selectively declare the static option itself using an intermediate macro [not 100% sure on the syntax for this one]
In all cases, the body of the function will still be there, but since you never call it it doesn’t really matter.