成员初始化列表 vs assignment/copy 构造函数(在 boost deadline_timer 中)

Member initialization list vs assignment/copy constructor (in boost deadline_timer)

我有以下 class 声明:

class A {
public:
    A();
private:
    boost::asio::io_service io;
    boost::asio::deadline_timer t;
};

class A 的以下构造函数工作正常:

A::A() : t(io) {
    // do stuff
}

但是当我写这个的时候:

A::A() {
    t(io);
    // do stuff
}

我收到以下错误:

error: no match for call to ‘(boost::asio::deadline_timer {aka boost::asio::basic_deadline_timer}) (boost::asio::io_service&)

可能是因为boost::asio::deadline_timer的复制构造函数没有定义。但是在成员初始化列表的情况下会发生什么?它不使用复制构造函数吗?

关于成员初始化列表中使用的机制与我们在 class 构造函数中使用 assignment/copy 构造函数时相比,我的问题也许可以更笼统。

我想我的问题有了答案。引用 cppreference:

Before the compound statement that forms the function body of the constructor begins executing, initialization of all direct bases, virtual bases, and non-static data members is finished. Member initializer list is the place where non-default initialization of these objects can be specified. For members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified.

所以在我的例子中,boost::asio::deadline_timer 的复制构造函数没有定义。因此我需要在初始化列表中构造它。

这个问题与拷贝构造函数无关,你根本没有调用它。重点是两个 t(io) 不是一回事。

成员初始化列表中的

t(io)表示构造函数以io为参数构造t。 (我想 boost::asio::deadline_timer 有一个以 boost::asio::io_service 作为参数的构造函数。)

t(io)在构造函数体中是一个语句,表示调用t作为一个函子,传递io作为它的参数。它失败了,因为 boost::asio::deadline_timer 不支持这种函子行为。