c++ - 用 constexpr 对象的函数指针成员函数初始化 std::array
问题描述
我正在尝试初始化函数指针的 std::array 。这些指针指向已实例化对象的成员函数。
有人可以帮忙看看下面的例子吗?提前谢谢了!
#include <array>
using TVoidVoid = void (*)(void);
class Foo {
public:
constexpr Foo() {}
void myHandler() {}
};
class Bar {
public:
constexpr Bar() : handler_{nullptr} {}
constexpr Bar(TVoidVoid handler) : handler_{handler} {}
private:
TVoidVoid handler_;
};
Foo f;
std::array<Bar, 5> bar_array = {{Bar{}, Bar{f.myHandler}}};
int main() {}
编译产生:
main.cpp:22:56: error: no matching function for call to ‘Bar::Bar(<brace-enclosed initializer list>)’
std::array<Bar, 5> bar_array = {{Bar{}, Bar{f.myHandler}}};
我正在使用g++ (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
.
解决方案
指向自由函数的指针的处理方式与指向成员函数的指针不同。TVoidVoid
类型是指向自由函数的指针,但您需要指向成员函数的指针Foo
。因此,Foo
首先定义,
class Foo { /* As before... */ };
然后使用成员函数的类型别名(Foo
此时必须知道)
// Note the different syntax to the former TVoidVoid
using FooVoidVoid = void (Foo::*)();
接下来,Bar
必须调整它的数据成员是类型FooVoidVoid
,并且构造函数接受这个类型作为参数(其余的Bar
可以保持原样),最后将数组定义为
std::array<Bar, 3> bar_array = {{Bar{}, Bar{&Foo::myHandler}}};
请注意,这与任何现有实例&Foo::myHandler
无关。Foo
它只是一个指向Foo
成员函数的指针,只有当你调用它时,它才必须与一个Foo
对象(特殊运算符.*
和->*
旨在实现这一点,或者std::invoke
在升级到支持 C++17 的编译器后使用) )。
推荐阅读
- node.js - 有没有办法在 mongodb 中对来自不同模型的多个集合进行搜索查询?
- azure-ad-b2c - AAD B2C 有条件的 MFA - 不注册
- node.js - React 客户端未通过 ngrok 连接到 Socket.io 服务器
- process - 操作系统中的上下文切换到底是什么意思?
- mysql - 如何在 sql 中获得这种枢轴类型的输出
- reactjs - 如何在材质 UI 中显示页码?
- android - 具有当前位置的 Location 实例
- jaxb - JAXB,具有多个命名空间的属性,编组时如何在它们之间进行选择
- swift - 核心数据 - 导入数据 (csv, xml)
- java - 仅使用类 String 的下一个方法以递归方式查找字符串的子字符串:charAt(),substring(),length()