chessbot
chessbot

Reputation: 436

Compiler-specific error: can't match function with const arguments

I'm pretty new to C++, so I'm trying to figure out exactly what's going on here. I'm trying to make (someone else's) code compile. It runs fine using mingw, but I'm also crosscompiling onto an embedded system (TS-7800) and running into compiler errors. It looks to me like somehow mingw does some auto matching with const that the crosscompiler doesn't, but I don't exactly know what's going on.

Here's the code:

typedef ::zmq::context_t ZeroMQContextType;
typedef boost::shared_ptr<ZeroMQContextType> ZeroMQContextHandleType;
typedef ::zmq::socket_t ZeroMQSocketType;
typedef boost::shared_ptr<ZeroMQSocketType> ZeroMQSocketHandleType;

SocketFactory::ZeroMQSocketHandleType SocketFactory::createZmqSocket(
    ZeroMQContextHandleType const & contextHandle, int const & zmqSocketType) {

    ZeroMQSocketHandleType socketHandle;

    switch (zmqSocketType) {
    case ZMQ_PUB:
        socketHandle = boost::make_shared<ZeroMQSocketType>(*contextHandle, ZMQ_PUB);
        // other stuff

    // etc ...
    }
return socketHandle;

}

And the error I'm getting:

/path/include/boost/smart_ptr/make_shared_object.hpp: In function `typename boost::detail::sp_if_not_array<T>::type boost::make_shared(const A1&, const A2&) [with T = zmq::socket_t, A1 = zmq::context_t,
SocketFactory.cpp:42:   instantiated from here
/path/include/boost/smart_ptr/make_shared_object.hpp:743: error: no matching function for call to `zmq::socket_t::socket_t(const zmq::context_t&, const int&)'
/path/include/zmq.hpp:395: note: candidates are: zmq::socket_t::socket_t(const zmq::socket_t&)
/path/include/zmq.hpp:278: note:                 zmq::socket_t::socket_t(zmq::context_t&, int)

I've tried removing the consts from the function, but it looks like they get added right back in when passing in the arguments to make_shared.

  1. Can someone explain to me what is going on with this error? And why does mingw not have a problem with this code? (I believe it has also worked fine on VS10 and standard gcc).
  2. Failing a good explanation, how can I fix this problem and move onto the next error?

Thanks!

Upvotes: 2

Views: 666

Answers (1)

chessbot
chessbot

Reputation: 436

OK, turns out reading the documentation for make_shared clears it up. Apparently make_shared passes only const references if C++0x support is detected:

http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/make_shared.html#functions

As indicated, the parameters can be wrapped in boost::ref to pass them non-const.

Upvotes: 2

Related Questions