Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for string_view #611

Open
Falgu9 opened this issue Aug 17, 2023 · 2 comments
Open

Support for string_view #611

Falgu9 opened this issue Aug 17, 2023 · 2 comments

Comments

@Falgu9
Copy link

Falgu9 commented Aug 17, 2023

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.

@gummif
Copy link
Member

gummif commented Aug 17, 2023

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).

@Falgu9
Copy link
Author

Falgu9 commented Aug 18, 2023

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants