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 1000017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:26:42+00:00 2026-05-16T07:26:42+00:00

I’m currently working on a reporting library as part of a large project. It

  • 0

I’m currently working on a reporting library as part of a large project. It contains a collection of logging and system message functions. I’m trying to utilize preprocessor macros to strip out a subset of the functions calls that are intended strictly for debugging, and the function definitions and implementations themselves, using conditional compilation and function like macros defined to nothing (similar to the way that assert() calls are removed if DEBUG is defined). I’m running into a problem. I prefer to fully qualify namespaces, I find it improves readability; and I have my reporting functions wrapped in a namespace. Because the colon character can’t be part of a macro token I am unable to include the namespace in the stripping of the function calls. If I defined the functions alone to nothing I end up with Namespace::. I’ve considered just using conditional compilation to block the function code for those functions, but I am worried that the compiler might not competently optimize out the empty functions.

namespace Reporting
{
    const extern std::string logFileName;

    void Report(std::string msg);
    void Report(std::string msg, std::string msgLogAdd);
    void Log(std::string msg);
    void Message(std::string msg);

    #ifdef DEBUG
        void Debug_Log(std::string message);
        void Debug_Message(std::string message);
        void Debug_Report(std::string message);
        void Debug_Assert(bool test, std::string message);
    #else
        #define Debug_Log(x);
        #define Debug_Message(x);
        #define Debug_Report(x);
        #define Debug_Assert(x);
    #endif

};

Any idea on how to deal with the namespace qualifiers with the preprocessor?
Thoughts on, problems with, just removing the function code?
Any other ways to accomplish my goal?

  • 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-16T07:26:43+00:00Added an answer on May 16, 2026 at 7:26 am

    This is how I did it when I wrote a similar library several months back. And yes, your optimizer will remove empty, inline function calls. If you declare them out-of-line (not in the header file), your compiler will NOT inline them unless you use LTO.

    namespace Reporting
    {
        const extern std::string logFileName;
    
        void Report(std::string msg);
        void Report(std::string msg, std::string msgLogAdd);
        void Log(std::string msg);
        void Message(std::string msg);
    
        #ifdef DEBUG
            inline void Debug_Log(std::string message) { return Log(message); }
            inline void Debug_Message(std::string message) { return Message(message); }
            inline void Debug_Report(std::string message) { return Report(message); }
            inline void Debug_Assert(bool test, std::string message) { /* Not sure what to do here */ }
        #else
            inline void Debug_Log(std::string) {}
            inline void Debug_Message(std::string) {}
            inline void Debug_Report(std::string) {}
            inline void Debug_Assert(std::string) {}
        #endif
    };
    

    Just as a side note, don’t pass strings by value unless you need to make a copy anyways. Use a const reference instead. It prevents an expensive allocation + strcpy on the string for EVERY function call.

    EDIT: Actually, now that I think about it, just use a const char*. Looking at the assembly, it’s a LOT faster, especially for empty function bodies.

    GCC optimizes this out at -O1, I don’t think there’s much of an issue with this:

    clark@clark-laptop /tmp $ cat t.cpp
    #include <cstdio>
    
    inline void do_nothing()
    {
    }
    
    int main()
    {
            do_nothing();
            return 0;
    }
    clark@clark-laptop /tmp $ g++ -O1 -S t.cpp   
    clark@clark-laptop /tmp $ cat t.s
            .file   "t.cpp"
            .text
    .globl main
            .type   main, @function
    main:
    .LFB32:
            .cfi_startproc
            movl    $0, %eax
            ret
            .cfi_endproc
    .LFE32:
            .size   main, .-main
            .ident  "GCC: (Gentoo 4.5.0 p1.2, pie-0.4.5) 4.5.0"
            .section        .note.GNU-stack,"",@progbits
    

    After a bit of tweaking, it seems that this will only be a FULL removal if you use const char*, NOT std::string or const std::string&. Here’s the assembly for the const char*:

    clark@clark-laptop /tmp $ cat t.cpp 
    inline void do_nothing(const char*)
    {
    }
    
    int main()
    {
            do_nothing("test");
            return 0;
    }
    clark@clark-laptop /tmp $ g++ -O1 -S t.cpp 
    clark@clark-laptop /tmp $ cat t.s
            .file   "t.cpp"
            .text
    .globl main
            .type   main, @function
    main:
    .LFB1:
            .cfi_startproc
            movl    $0, %eax
            ret
            .cfi_endproc
    .LFE1:
            .size   main, .-main
            .ident  "GCC: (Gentoo 4.5.0 p1.2, pie-0.4.5) 4.5.0"
            .section        .note.GNU-stack,"",@progbits
    

    And here’s with const std::string&…

            .file   "t.cpp"
            .section        .rodata.str1.1,"aMS",@progbits,1
    .LC0:
            .string "test"
            .text
    .globl main
            .type   main, @function
    main:
    .LFB591:
            .cfi_startproc
            subq    $24, %rsp
            .cfi_def_cfa_offset 32
            leaq    14(%rsp), %rdx
            movq    %rsp, %rdi
            movl    $.LC0, %esi
            call    _ZNSsC1EPKcRKSaIcE
            movq    (%rsp), %rdi
            subq    $24, %rdi
            cmpq    $_ZNSs4_Rep20_S_empty_rep_storageE, %rdi
            je      .L11
            movl    $_ZL22__gthrw_pthread_cancelm, %eax
            testq   %rax, %rax
            je      .L3
            movl    $-1, %eax
            lock xaddl      %eax, 16(%rdi)
            jmp     .L4
    .L3:
            movl    16(%rdi), %eax
            leal    -1(%rax), %edx
            movl    %edx, 16(%rdi)
    .L4:
            testl   %eax, %eax
            jg      .L11
            leaq    15(%rsp), %rsi
            call    _ZNSs4_Rep10_M_destroyERKSaIcE
    .L11:
            movl    $0, %eax
            addq    $24, %rsp
            .cfi_def_cfa_offset 8
            ret
            .cfi_endproc
    .LFE591:
            .size   main, .-main
            [Useless stuff removed...]
            .ident  "GCC: (Gentoo 4.5.0 p1.2, pie-0.4.5) 4.5.0"
            .section        .note.GNU-stack,"",@progbits
    

    Huge difference, eh?

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

Sidebar

Related Questions

Currently, I am working on system that does quite a bit of reporting-style functions
I am currently working on moving a large project over to use Common.Logging and
I am working on a reporting project, and the client currently makes use of
I am currently working on a project where reporting services are required. The database
Im currently working with an API which requires we send our collection details in
Im currently working on a site that you can upload large files.. on local
Im currently working on a grizzly, spring and jersey project and i have encountered:
I am working on a rather large project (multiple teams) so I don't have
I'm working rearchitecting a reporting/data warehouse type database. We currently have a table that
I'm currently working on a project in which there are about 20 c source

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.