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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:20:53+00:00 2026-06-09T04:20:53+00:00

My application uses another output than the standard output for logging information, which is

  • 0

My application uses another output than the standard output for logging information, which is why I wrote my own Log(), Error(), Panic() and Assert() functions. To organize things nicely, I enclose all the debugging stuff in a Debug namespace.

It would make more sense for the Assert() function to also provide a source file and line number, which is only possible using the __LINE__ and __FILE__ macros. However, it is pretty unpleasant, inefficient etc… to always have to specify these two parameters.

So this is how my code would look like:

namespace Debug {
   void Assert (int condition, std::string message, std::string file, int line);
}

My question is, is it possible to place a macro which includes those two parameters inside the Debug namespace? Like this:

namespace Debug {
   void Assert_ (int condition, std::string message, std::string file, int line);
   #define Assert(a,b) Assert_(a, b, __FILE__, __LINE__)
}

// .... Somewhere where I call the function ....
Debug::Assert (some_condition, "Some_condition should be true");

// Output: Assertion failed on line 10 in file test.cpp:
//           Some_condition should be true

Is this valid c++? If not, is there any way of making this work?

  • 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-06-09T04:20:54+00:00Added an answer on June 9, 2026 at 4:20 am

    Is it possible to place a macro in a namespace in c++?

    No.

    #define is a preprocessor directive. The macros are being replaced before anything else apart from removing comments (which means, before compilation). So at the time macros are replaced, the compiler knows nothing about your namespaces.

    As other people state, in your case it will be fine. However, This is how you can get problems:

    namespace A
    {
     void Assert_ (int condition, std::string message, std::string file, int line)
     {
         std::cout << "A";
     }
       #define Assert(a,b) Assert_(a, b, __FILE__, __LINE__)
    
    }
    namespace B
    {
     void Assert_ (int condition)
     {
         std::cout << "B";
     }
       #define Assert(a,b) Assert_(a)
    
    }
    
    int main(int argc, char *argv[])
    {
        A::Assert(0,"asdasd");
        B::Assert(0,"asdasd");
    }
    

    So while it looks like the defines are "in the namespaces", they are not, and the last #define will be always be used, which in this case will lead to a compile-time error, because the code in main will be replaced by:

    A::Assert(0);
    B::Assert(0);
    

    instead of

    A::Assert(0,"asdasd", _FILE_, _LINE_);
    B::Assert(0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to parse UTF8 characters from the standard output stream of another application
My application uses a MapView which will display multiple places. I have the latitude
My application uses HttpURLConnection to connect to my REST services. I log errors and
My application uses MS access database to save some sensitive information. Though that information
TickZoom is a very high performance app which uses it's own parallelization library and
I am building a WP7 application which heavily uses RX and I came across
My java application uses log4j for logging. Using ant the project builds successfully, but
I need to connect to another web application using webservice. That web application uses
ORIGINAL QUESTION Our application uses CSocket which requires the message pump to be running
My application uses Google to search for information and retrieves it. After a few

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.