Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 308
Tibi Avatar asked Aug 03 '12 07:08

Tibi


People also ask

Can macros be Namespaced?

Yes, and your macro would expand to exactly what you expect.

How do you declare a macro in C?

A macro is a fragment of code that is given a name. You can define a macro in C using the #define preprocessor directive. Here's an example. Here, when we use c in our program, it is replaced with 299792458 .

Can we define macros in main function in C?

The Concept of C Macros Macros are generally used to define constant values that are being used repeatedly in program. Macros can even accept arguments and such macros are known as function-like macros. It can be useful if tokens are concatenated into code to simplify some complex declarations.

Can namespaces be used in C?

Namespace is a feature added in C++ and is not present in C. A namespace is a declarative region that provides a scope to the identifiers (names of functions, variables or other user-defined data types) inside it.


1 Answers

#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); 
like image 118
SingerOfTheFall Avatar answered Sep 24 '22 12:09

SingerOfTheFall