-
Notifications
You must be signed in to change notification settings - Fork 60
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 C++17 std::string_view in C++ wrapper #110
Comments
I've never really used string_view myself so I'll have to experiment a bit, but it's a nice idea. ;) The reason for the custom string type was to seamlessly support conversion from integers to strings (for specifying port in Address constructor) as well as to accept C-style and C++ strings, of course, without having to override each one. The natural thing in that case would be your last suggestion, to just accept string_view as an additional conversion. However, if that's not a desirable approach, I would look into at least what is the "normal" way for a C++17 function to accept both std::string or std::string_view, and move to that. (Although technically liblo doesn't require C++17 yet.. it would be nice to be backward compatible to C++11 but hey.) I don't know if C++17 might have more convenient ways to deal with number to string conversion, maybe some kind of std::variant, or a template approach, could be better here. After a short search, I'm not clear on how to properly support string_view. It seems like functions need to be changed to accept So, due to that last point, maybe forcing the user to convert to |
Generally, one should use just
Well, there is class string_type
{
template <typename T>
string_type(const T v) : string_type(std::to_string(v))
{}
template <>
string_type(const std::string& s);
template <>
string_type(const char* s);
string_type(std::string_view);
};
Yeah, this issue could be solved by providing a custom
Good point. Really haven't had that in mind, to be honest. I've just did some research and passing a
Not really, at least not if the user passes Well, now that I know that |
I played around a bit with trying to convert What do you think of this approach? https://github.com/radarsat1/liblo/tree/add-support-for-string_view |
The approach looks fine to me. Additionally, |
Okay, yeah I'll test out removing |
I noticed some changes related to this issue were included in the 0.32 release but they are from 4 years ago, so not confident whether this issue was resolved or still being worked on. |
I just noticed that using
std::string_view
is not very convenient when working with liblo's C++ wrapper as it contains its ownstring_type
which has no support forstd::string_view
. One needs to passstd::string_view::data()
instead of the actualstd::string_view
to any function requiring astring_type
.The best option might be to replace
string_type
withstd::string_view
altogether, but this might introduce either the need to disable support for C++14 and below or to introduce an interface tostring_type
which is similar tostd::string_view
. The latter option might be better as the required interface needed seems to be only::data()
returning aconst char*
. In that case, it should be sufficient to check for the C++ version and use eitherusing string_type = std::string_view
or the own implementation.The final option, which is probably the easiest to implement, but (in my opinion) the worst of the all, is to just accept
std::string_view
as parameter instring_type
's constructor. This is, however, kinda like reinventing the wheel asstd::string_view
andstring_type
kind of do the same.I might find some time implementing this myself and providing a PR, but in the meantime, feel free to add your opinion to this. If this is not a wanted enhancement, please let me know.
The text was updated successfully, but these errors were encountered: