简体   繁体   中英

__func__ in linux vs __FUNCTION__ in VS

class A 
{
 public:
 void Print()
 {
   #if defined( win32 )
   std::cout << __FUNCTION__ << std::endl;
   #else
   std::cout << __func__ << std::endl;
   #endif
 }
};

int main()
{
 A ob;
 ob.Print();
 return 0;
}

The above code snippet prints A::Print in Windows and Print in Linux. What's the way to get the the classname::functionname in Linux ?

There is no macro, you are looking for. But you may easily do it from __PRETTY_FUNCTION__ like:

inline std::string 
method_name (const std::string &fsig)
{
  size_t colons = fsig.find ("::");
  size_t sbeg = fsig.substr (0, colons).rfind (" ") + 1;
  size_t send = fsig.rfind ("(") - sbeg;
  return fsig.substr (sbeg, send) + "()";
}

#define __METHOD_NAME__ method_name (__PRETTY_FUNCTION__)

And then use like:

#if defined (win32)
std::cout << __FUNCTION__ << std::endl;
#else
std::cout << __METHOD_NAME__ << std::endl;
#endif

To get identical results

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM