Search code examples
c++boostmember-initialization

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


I have the following class declaration:

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

The following constructor for class A works fine:

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

But when I write this:

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

I get the following error:

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

Perhaps because the copy constructor for boost::asio::deadline_timer is not defined. But what is happening in case of member initialization list? Does it not use the copy constructor?

My question perhaps can be made more general in regard to what is the mechanism used in member initialization lists vs when we use assignment/copy constructor inside the class constructor.


Solution

  • The issue has nothing to do with copy constructor, and you didn't call it at all. The point is the two t(io)s are not the same thing.

    t(io) in member initialization list means construct t by the constructor taking io as its argument. (I suppose boost::asio::deadline_timer has a constructor taking boost::asio::io_service as parameter.)

    t(io) in the body of constructor is a statement, means calling t as a functor, passing io as its argument. It fails because boost::asio::deadline_timer doesn't support such functor behavior.