c++ 11: std::mem_fn的类型名
C++11 : type name for std::mem_fn
auto
很好,但我需要在类中声明成员,而不是堆栈中的变量。
decltype
工作,但不知何故看起来很奇怪
class Automation {
void _init_state(int);
decltype(std::mem_fn(&Automation::_init_state)) next_state;
};
std::function
似乎也可以工作,但与纯成员函数
class Automation {
void _init_state(int) {}
public:
decltype(std::mem_fn(&Automation::_init_state)) next_state;
std::function<void(Automation&, int)> next_state_fn;
Automation()
: next_state(&Automation::_init_state)
, next_state_fn(&Automation::_init_state)
{}
};
int main()
{
/* on ubuntu, x64 */
std::cout << sizeof Automation::next_state << std::endl; /* 16 */
std::cout << sizeof Automation::next_state_fn << std::endl; /* 32 */
return 0;
}
谁能告诉我正确的方法是什么? 标准未指定std::mem_fn
的返回类型,因此没有可移植的方法来显式声明该类型的成员变量。
虽然decltype
结构可能看起来很奇怪,但这是正确的方法。std::function
会产生一些开销,但是更灵活,因为可以比使用decltype
版本更容易地传递它。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一
- c++调用约定是否受标准约束,因为在声明fn时不需要定义函数的返回类型