2

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.

Jonathan Sternberg
  • 6,421
  • 7
  • 39
  • 58
  • possible duplicate of [Conditional compile-time inclusion/exclusion of code based on template argument(s)?](http://stackoverflow.com/questions/5659064/conditional-compile-time-inclusion-exclusion-of-code-based-on-template-arguments) – Xeo Jun 05 '11 at 00:41
  • It's a duplicate, but sadly there seems to be no solution to this. :( – Xeo Jun 05 '11 at 00:42
  • It's unclear exactly what you want to enable or disable. You show an example of a global variable, but your description involved enabling or disabling code. And somehow macros are involved. Can you provide a minimal example of this "cannot put `#if 0` inside a macro" problem you're having? – Ben Voigt Jun 05 '11 at 01:04

4 Answers4

3

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
1

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.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • The reason is because I wanted it to be used for a unit testing library where the tests are embedded to run before the main function, but would be removed during a release build (as not to effect performance or crashing). Similar to how D handles unit tests. So this isn't about unresolved issues, but making it so running the program is the same as running the unit tests and optionally disabling that. – Jonathan Sternberg Jun 05 '11 at 13:47
  • @Jonathan Sternberg: This makes it a much more interesting problem :) – johnsyweb Jun 06 '11 at 02:21
0

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 MyObjectbased on the value of the constant-expression debug.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • The other one you can use is `enable_if`: http://www.boost.org/doc/libs/release/libs/utility/enable_if.html – Sean Jun 05 '11 at 01:10
0

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.

Dennis Zickefoose
  • 10,791
  • 3
  • 29
  • 38