Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5990549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:15:35+00:00 2026-05-22T23:15:35+00:00

Is it possible to selectively compile in certain sections of code with templates, or

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:15:36+00:00Added an answer on May 22, 2026 at 11:15 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to selectively disable the autofill feature in text fields using code?
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Is it possible to use regular exprssions in css to selectively hide text? I
Possible Duplicate: Difference between Convert.tostring() and .tostring() Hi Carrying on from this question What
I know its probably possible, but is it practical and doable to try and
I know its probably possible, but is it practical and doable to try and
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.