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 bui开发者_如何学JAVAld. 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.
You need preprocessor conditionals to get rid of declarations. But if you just want to enable/disable a block in the middle of a function, that's no problem. You could use template specialization, but the simplest thing is just to use if (_DEBUG) { ... }
, in a release build the compiler will optimize away the dead code.
Traditionally, "debug" code would be wrapped in a block like this, using the preprocessor rather than the compiler:
#ifdef DEBUG
// Some debugging code here...
#endif
...and passing -DDEBUG
into the preprocessor only for "debug" builds.
It would better, however, to address the differences between "debug" and "release" builds. Debug blocks are usually an indicator of unresolved issues.
My recommendation is that you remove these conditions entirely.
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 #ifdef
block there, and chose how you define it.
#ifdef NDEBUG
#define unittest(NAME) static void dummy_func_##NAME()
#else
#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()
#endif
You could also use a single definition there, and change main
to
int main() {
#ifndef NDEBUG
unit_tests::run_all_tests(); //or whatever
#endif
//regular old code
}
A final option would be to selectively declare the static option itself using an intermediate macro [not 100% sure on the syntax for this one]
#ifndef NDEBUG
#define DECLARE(NAME) static unittest_##NAME NAME##_unittest;
#else
#define DECLARE(NAME) /* noop */
#endif
#define unittest(NAME) \
struct unittest_##NAME { /*add internals*/ }; \
DECLARE(NAME); \
void unittest_##NAME::run_test()
In all cases, the body of the function will still be there, but since you never call it it doesn't really matter.
You can use something like conditional metafunction from Boost.
boost::mpl::if_c <debug, MyDebuggingClass, EmptyClass>::type MyObject;
This selects the type of the variable MyObject
based on the value of the constant-expression debug
.
精彩评论