cppzmq icon indicating copy to clipboard operation
cppzmq copied to clipboard

Support for string_view

Open Falgu9 opened this issue 2 years ago • 2 comments

Version of cppzmq : 4.10.0 Compiler version : 9.4.0 C++ version : 17 I mainly use std::string_view to handle string into my functions and I would like to be able to use them without conversion. Before :

std::string_view url = "url";
socket.connect(url.data());

After :

std::string_view url = "url";
socket.connect(url);

Here the error :

src/network/SubNetwork.cpp:11:24: error: no matching function for call to ‘zmq::socket_t::connect(std::string_view&)’
     socket_.connect(url);
                        ^
In file included from src/network/Network.h:5,
                 from src/network/SubNetwork.h:4,
                 from src/network/SubNetwork.cpp:1:
./cppzmq/zmq.hpp:1894:10: note: candidate: ‘void zmq::detail::socket_base::connect(const string&)’
     void connect(std::string const &addr) { connect(addr.c_str()); }
          ^~~~~~~
./cppzmq/zmq.hpp:1894:10: note:   no known conversion for argument 1 from ‘std::string_view’ {aka ‘std::basic_string_view<char>’} to ‘const string&’ {aka ‘const std::__cxx11::basic_string<char>&’}
./cppzmq/zmq.hpp:1896:10: note: candidate: ‘void zmq::detail::socket_base::connect(const char*)’
     void connect(const char *addr_)
          ^~~~~~~
./cppzmq/zmq.hpp:1896:10: note:   no known conversion for argument 1 from ‘std::string_view’ {aka ‘std::basic_string_view<char>’} to ‘const char*’

This is also a problem for bind or zmq::str_buffer. I would like to try to get it done if possible.

Falgu9 avatar Aug 17 '23 08:08 Falgu9

The libzmq function take null terminates strings so string views must be converted to strings. Your before example is buggy if the underlying string is not null terminated, use std::string(url) instead. str_buffer is for string literals only. Use zmq::buffer instead (I think that works).

gummif avatar Aug 17 '23 18:08 gummif

Oh okay thanks but maybe it might be a good idea to force the good habits like :

void connect(std::string_view addr) { connect(std::string(addr)); }

Same for bind, disconnect, etc...

Falgu9 avatar Aug 18 '23 12:08 Falgu9