zeromq / cppzmq

Header-only C++ binding for libzmq
http://www.zeromq.org
MIT License
1.98k stars 756 forks source link

sockopt::rcvtimeo argument too small #642

Open jwmelto opened 1 month ago

jwmelto commented 1 month ago

I'm updating legacy code to use the new socket options, and ran into an issue:

error: no matching function for call to 'zmq::socket_t::set(const zmq::sockopt::rcvtimeo_t& std::chrono::duration<long int, std::ratio<1, 1000> >::rep)'

The calling user code is like this:

void Send(zmq::socket_t& sock, std::chrono::milliseconds timeout) {
    sock.set(zmq::sockopt::rcvtimeo, timeout.count());
...
}

A richer interface would accept a (templated?) std::chrono::duration, but the specific issue here is the definition of sockopt::rcvtimeo uses int and template substitution fails when provided a long int (std::chrono::milliseconds::rep).

Simply changing the definition of rcvtimeo to take a long int breaks code like

sock.set(zmq::sockopt::rcvtimeo, 42);

and the enumeration-as-helper-type pattern precludes overloads. I don't see a good solution to this issue

gummif commented 1 week ago

Thanks for that. It would be possible to add an overload for durations, something like

    template<class T>
    void set(sockopt::integral_option<ZMQ_RCVTIMEO, int, false>, duration<T> val)
    {
        // ...
    }