Hi,
I wrote the following macro:

#define COM "
#define GET_VAR_LINE(var, type) COM var = %type COM, var

So, this code :

int main()
{
        int x = 0;
        printf(GET_VAR_LINE(x, d));
}

expands to

int main()
{
 int x = 0;
 printf(" x = %d ", x);
}

Acquired with g++ -E main.cpp

How do I eliminate the spaces at the beginning and the end of
" x = %d " ? Thanks.

Fortunately, you can't define these weird macros.

Macro definition consists of tokens (valid C++ lexical units) - it's so called token-string in the C++ standard. In other words, it's not an arbitrary sequence of characters. No such token as a single quote mark in C++. It starts a string literal - but no string literal on #define COM line...

The answer to OP question: see "token-pasting" operator ##. For example:

#define BEAST(head,body,tail) head##body##tail

No spaces between parts of this (useless) macros...

Back to OP macros:
Are you sure that an inexpressive and unintelligible code with complicated macros is the true indication of a good programmer?

Can anyone tell me the answer? Or if it can not be done?

>Can anyone tell me the answer? Or if it can not be done?
ArkM has beautifully answered your question. Open your mind to see this.

I would again like to point out ArkM's last point: If you are a C++ programmer, you should know that Macros are evil. Look at (http://siddhant3s.uuuq.com/how_to_tell_rusted_cpp/#SECTION00051000000000000000)
and (http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.5)

If you are a C programmer, you should not post C related queries in C++ forums.

OK. Let me phrase the question in a different manner:
I am going through a fairly large program written by someone else, and I thought it'd make it easy for me if I can just dump objects right into the log. So,

class Test
{
int x;
...

PrintInfo();
};

would print something like

"variable x = 10" in the log.

Is there a easy way to do this?

class Log
{
public:
  Log(const char* fname);
  ...
  static ostream& curr()
  {
    return Log::flog << Log::timeStamp() << ':';
  }
  ...
private:
  static const char* timeStamp();
  static ofstream flog;
  Log(const Log&);
  Log& operator=(const Log&);
};

inline 
ostream& Log() {  return Log::curr(); }

/// My days are numbered if no macros
#define VAR(var)  " "#var" = " << var

class Test
{
  int x; ...
  ...
  ostream& logInfo()
  {
    return Log() << "Test::logInfo:"
      << VAR(x) << VAR(y) //<< ...
      << '\n';
  }
};
...
Log() << test.logInfo() << VAR(its.me) << endl;
...
This article has been dead for over six months. Start a new discussion instead.