C++:std::bind() 如何"知道"模板参数是指静态函数还是成员函数?
c++ : how does std::bind() 'know' whether the template args refer to a static function or member function?
使用 bind() 我可以做到这两点:
void f(int a) { }
class C {
public:
void f (int a) { }
};
int main()
{
auto f1 = std::bind (f,3);
f1();
C myC;
auto f2 = std::bind (&C::f, &myC, 3);
f2();
}
大概在下面,f1()以某种方式被翻译成f(3),f2()被翻译成(&myC)->f(3)。我不太关心"3"参数的绑定,但我想了解 bind() 如何自动知道 f1 应该只是一个直接的函数调用,而 f2 应该是一个对象的成员调用。在编译时,用于检查第一个参数的"风味"是什么技术是什么?我想在我自己的程序中利用这种技术。
一种简单、打包的方法是使用
std::is_member_function_pointer<F>::value
其中F
是第一个参数的类型,可能删除了潜在的引用和 CV 限定。
相关文章:
- 将公共但非静态的成员函数与ALGLIB集成
- 虚拟成员函数的定义是否强制在同一转换单元中动态初始化静态数据成员?
- 错误: 无效使用非静态数据成员"应用程序::应用程序构造函数"
- 指向模板上下文中的成员函数或静态函数的指针
- 在模板定义中调用非静态constexpr成员函数
- 如何在静态函数中使用成员函数数组
- (2 问题)"类"类型重新定义(即使 #pragma 一次),以及静态函数内的静态成员对象初始化?
- 使用 lambda 函数初始化静态数据成员
- 学习C++并在早期示例中遇到错误(在非静态数据成员之前需要构造函数)
- 静态模板成员函数的实例化?
- "非静态数据成员之前需要构造函数" - 我是否使用"boost::variant"
- uninit_member:非静态类成员字段 m_cJobState.bstatus 未在此构造函数中初始化,也不在其调
- uninit_member:非静态类成员m_wszParams未在此构造函数中初始化,也不在它C++中调用的任何函数中初
- 说明通过指针访问非静态类成员函数
- 通过呼叫constexpr函数来定义静态constexpr成员
- 当使用嵌套类功能时,使用非静态数据成员的使用无效,但是当函数未固定时可以
- 使用静态成员函数访问静态数据成员
- 是否应该在复制构造函数或赋值运算符中复制静态数据成员
- C++11 静态函数成员变量
- 类C++的静态函数成员中的静态数据成员初始化顺序