2014-01-13 110 views
0
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; 
} 

上面的代码片断输出A::Print在Windows和Linux中Print。 在Linux中获得classname::functionname的方式是什么?__func__在linux VS __FUNCTION__在VS

+0

GCC有'__FUNCTION__'宏。此外,您可以使用'__PRETTY_FUNCTION__',它与'__FUNCTION__'不同,因为'__PRETTY_FUNCTION__'也包含参数子句。 – ForEveR

+0

看起来像gcc中的__FUNCTION__也只给出了函数名,__PRETTY_FUNCTION__返回了整个函数签名:(我的要求是不同的 – KodeWarrior

回答

0

没有宏,你正在寻找。但是你可以很容易地从__PRETTY_FUNCTION__喜欢做:

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__) 

然后用这样的:

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

要得到相同的结果