zeromq / cppzmq

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

Problem: `sock.get(zmq::sockopt::type)` is not typesafe #523

Closed jasujm closed 2 years ago

jasujm commented 2 years ago

Solution: Add zmq::sockopt::socket_type that is used to get() an enumerator from zmq::socket_type instead of plain int.

It makes code like this compile:

include

include

int main() { zmq::context_t ctx; zmq::socket_t sock(ctx, zmq::socket_type::push); assert(sock.get(zmq::sockopt::socket_type) == zmq::socket_type::push); return 0; }

This patch solves https://github.com/zeromq/cppzmq/issues/522

coveralls commented 2 years ago

Pull Request Test Coverage Report for Build 1369079373


Totals Coverage Status
Change from base Build 1266731609: 0.0%
Covered Lines: 801
Relevant Lines: 931

💛 - Coveralls
gummif commented 2 years ago

Looks good, thanks.

XieJiSS commented 2 years ago

Hi @gummif , I encountered this error when trying to #include "zmq.hpp":

// This is the macro expansion result
using socket_type_t = integral_option<16, socket_type, false>; inline constexpr socket_type_t socket_type {}
// The error
"zmq::sockopt::socket_type" is not a type name

According to my linter, this is caused by https://github.com/zeromq/cppzmq/blob/a98fa4a91d868a3844e5456741d6782cc1a8d98b/zmq.hpp#L1652

My workaround is:

// socket_type seems to be resolved to zmq::sockopt::socket_type instead of the enum class zmq::socket_type
ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TYPE, socket_type, zmq::socket_type);

I'm not very familiar with cpp, so I'm wondering whether this should be considered as a bug introduced by the PR, or an issue caused by possible misconfiguration?

My environment: #define CPPZMQ_LANG _MSVC_LANG which extends to 201703L and #define _MSC_VER 1929