如何判断当前函数是C++11中的正则成员函数还是静态成员函数
How to tell whether the current function is a regular or a static member function in C++11?
我将尝试解释我要做的事情:
bool if_filter_applies() {return true;}
#defile LOGFILE if( if_filter_applies() ) LOG_STREAM_OBJECT
void globalFunc() { LOGFILE << "Some data n"; }
class C {
int a;
bool if_filter_applies() {
if ( a == 1)
return true;
else
return false;
}
public:
void regMem () {
LOGFILE << "Some datan";
}
static void staticMem() {
LOGFILE << "Some datan";
}
};
我试图修改LOGFILE定义,使其仅在基于if_filter_applies()
成员函数的输出从类的成员函数中使用时写入流。
如果从类外或静态成员函数中使用LOGFILE,我希望它使用全局if_filter_applies()
函数(它总是返回true)。
上面的代码没有编译,因为static void staticMem()
最终使用if_filter_applies()
类成员而不是全局成员。
我不想创建类似于#define LOGFILE
的不同定义来替换静态成员函数,因为我们的代码中有数百个文件,我不想手动替换所有出现的文件。
那么,我可以对#defile LOGFILE
宏进行任何更改,以便它在静态成员函数的上下文中调用::if_filter_applies()
吗?
如果您使用的是MS Visual Studio,则可以对this
使用__if_exists
检查。所以类似于:
__if_exists(this)
{
// In a member function
}
__if_not_exists(this)
{
// Not in a member function
}
相关文章:
- 如何使用指针传递给函数的数组中对象的函数成员
- c++构造函数成员初始化:传递参数
- 创建 std::函数,它返回具有函数成员值的变量.分段错误
- 如何在C++通过公共函数访问私有函数成员?
- 解释了构造函数成员初始化列表
- 调用std::函数成员时内存损坏
- 是否可以为模板类的模板函数成员设置别名?
- 捕获 lambda 函数C++成员变量
- 构造函数成员初始值设定项跨成员列出,安全吗?
- 获取与在模板参数中传递的函数成员类型相同的类
- 如何从公共函数成员访问地图私有成员
- C 构造函数成员分配优化
- 使用命名空间进行函数成员定义
- 函数成员作为 CUDA 内核的参数
- 模板基类函数成员的别名
- 函数成员中用于void和继承的enable_if
- 头文件中是否定义了一个很长的Class函数成员
- 类内/构造函数成员初始化
- 使用指向部分专用函数成员的指针自动填充向量
- 指向函数成员的指针