-
Notifications
You must be signed in to change notification settings - Fork 98
Add string_view getters and setters #27
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
Conversation
In Agent, Key, and Query; add functions to set the string using a std::string_view. Remove the const char * version. Key::set_str(const char *) => set_str(string_view) Query::set_str(const char *) => set_str(string_view) Agent::set_key(const char *) => set_key(string_view) Agent::set_query(const char *) => set_query(string_view) Also add getters: Key::str() -> string_view Query::str() -> string_view These are convenient because string s; agent.set_key(s.data(), s.length()); becomes string s; agent.set_key(s); Setting with a const char * still works, since string_view has an implicit const char * constructor. Similarly, string_view(agent.key().ptr(), agent.key.length()) == x becomes agent.key().str() == x std::string_view is a C++17 feature, so all changes are guarded with #if __cplusplus >= 201703L.
Although const could prevent bugs, it's inconsistent with other function definitions, so remove it.
https://stackoverflow.com/questions/47240672/can-i-construct-a-string-view-from-a-nullptr had lead me to believe that string_view(nullptr, 0) was not legal, but nothing in iterator.requirements.general seems to exclude that: https://stackoverflow.com/questions/50454342/what-does-ptr-ptrlen-must-be-a-valid-range-mean Here is the proposal where it was allowed: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3921.html
This is shorter and makes the implementation parallel to set_query.
|
I agree with that For example, if libmarisa is built on C++17 or later, callers must be built on C++17 or later. |
There shouldn't be any confusion. You can still pass a Maybe I should add a comment like, "Don't worry, you can still pass
This is a good point. Shall I make it a configure option? |
|
How about adding accessors with new names, such as |
To me, it seems nicer to have the |
|
If I understand correctly, C++ doesn't have that kind of stdlib ABI compatibility. If Marisa was linked against C++17 stdlib, it is not guaranteed to work with C++11 stdlib even if new C++17 stuff isn't used. Am I wrong? |
|
If we make The same version of gcc with different I think if we did something like it will even work with both versions, since we're just adding a non-virtual function and not changing the representation. I'm not really sure, since I haven't thought about it enough, though. |
|
If If libmarisa is built on C++17 or later, |
Always provide these for all C++ versions and only add set_str(string_view) for C++17. This should provide binary compatibility if libmarisa is built with C++17, but could still lead to confusion if built with earlier standards, then used in something built with C++17.
Why is it better if it's inline?
Hmm. I fixed this, but now if the user build Maybe |
I'm sorry that I could not explain it well. I would like to show you an example. #ifndef ECHO_H
#define ECHO_H
#include <string_view>
class Echo {
public:
#if __cplusplus >= 201703L
std::string_view echo(std::string_view s) {
return s;
}
#else // __cplusplus >= 201703L
const char *echo(const char *s);
#endif
};
#endif // ECHO_H#include "echo.h"
#if __cplusplus < 201703L
const char *Echo::echo(const char *s) {
return s;
}
#endif // __cplusplus < 201703L// main.cc
#include <iostream>
#include "echo.h"
int main() {
Echo echo;
std::cout << echo.echo("Mike") << std::endl;
return 0;
}If echo.cc is compiled with C++17 and main.cc is compiled with C++14, the linker fails due to undefined reference as follows: This is because If the definition of
I think it's not a good way. |
|
That makes sense, thanks. 2139f49 no longer has that problem, since I only add functions, not delete them, but I'll do something like your echo but with inline. |
|
See if #31 is ok, then I'll do the inline version as you suggest. |
|
I think the current version of this PR (without #31 ) works, with a tiny disadvantage of larger binary size. It's equivalent to this version of echo:
$ g++ -c -std=c++17 echo.cc && g++ -c -std=c++14 main.cc && g++ echo.o main.o && ./a.out
MikeCan we proceed with this and decide whether/how to make |
|
Sorry for my late response. It looks good to me. |
In
Agent,Key, andQuery; add functions to set the stringusing a
std::string_view. Remove theconst char *version.Also add getters:
These are convenient because
becomes
Setting with a
const char *still works, sincestring_viewhas animplicit
const char *constructor.Similarly,
becomes
std::string_viewis a C++17 feature, so all changes are guardedwith
#if __cplusplus >= 201703L.