-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
misc/cpp/imgui_stdlib.h (was: misc/stl/imgui_stl.h) #2035
Comments
I'm not entirely sure but I think I have been bitten by this before. Isn't casting away const as you do with (char*)data.c_str() undefined behavior? |
I think this should work with any modern implementation that's not ref-counted (with shared non-empty data). A quick google essentially leads to "C++11 added specific language forbidding std::string from being reference counted". I'd be happy to hear about any concrete problem if any - there probably will be some - but I'd rather avoid getting too deep in the game of C++-theoretical-what-ifs without concrete evidence. As I understand, .data[0] provide even less guarantees, aka no guarantee to have a null-terminator (though in practice it will be the same). |
I just brought it up as a heads up as I remember doing something like it and having msvc doing something "smart" that GCC and clang didn't do :( unfortunately I don't remember more clearly than that and that is not much help I know so feel free to ignore. |
Will keep in mind, thanks! |
Honestly writing to the internal buffer of a This is not a problem anymore with C++11 since the standard guarantees the internal buffer of a string is null-terminated. This means using Anyway, in the end, you can always use macros to guard against users including this code pre-C++11 if you want to be on the safe side of things... |
Thanks for this, maybe with this I'll be able to remove all my extensions to ImGui. :) @jdumas agreed. @ocornut, There's no full-proof way to modify internal buffer pre-C++11, sadly. There's no guarantee that the buffer will be null-terminated and it's not even guaranteed to be contiguous! So I think we need to warn users about C++11 requirement... As for the So, to be perfectly safe, we either need to make an assert which will check that the passed string is not empty, or do "reserve" for at least 1 char to make By the way, I think it'll be better to not use "STL", but some for of "C++ std" in namings. Please, read this on topic of "STL" vs "std", but the main idea is: "STL" =/= "standard library" and most people in C++ world move away from referring to the Standard Library as "STL". |
As soon as we dip an inch into C++ the bike-shedding starts: the code do not use I think Jeremie's post established that it's guarantee to work with C++11. I will add a comment suggestion it's only guaranteed with C++11 and requesting feedback on pre-C++11 setups (which are very unlikely to ever be submitted.. the only people who are still working on pre-C++11 setups are the kind of code base which not using std::string). I am modifying the header to say:
Naming: The problem I have with "std" ("standard") is that it is obvious in the context of C++ but imgui is not only used in a C++ context, and using the word "standard" for anyone else not in this ecosystem is misleading. A better name would be |
Sorry, I was just thinking that &s[0] might be better, but it seems like c_str() is actually fine in C++11. I think the name "imgui_cpp_std" might be even better. :) (remember libstdc++ for example) |
Well if we're being picky about names then maybe |
Agreed, imgui_stdlib is perfect. |
The sample function InputTextCallback does not return any value, although the function pointer ImGuiInputTextCallback expects an int to be returned? The calling code does not check the return value either. |
Hello @aCuria, |
Hello @ocornut , What i meant is that the sample code (InputTextCallback) you posted in this thread does not return a value:
It is not clear what the return value should be, and the code in ImGui that calls this callback function also does not make use of the integer return value. I was thinking that if the str->resize() function fails then perhaps InputTextCallback should return 1 to indicate that the ImGuiInputTextFlags_CallbackResize callback has failed. One other minor detail is that you can use str->data() instead of (char*)str->c_str() |
The callback return value is only used for ImGuiInputTextFlags_CallbackCharFilter, where a value of 1 means discard. So I would return 0. |
Yes the full code always returns 0: https://github.com/ocornut/imgui/blob/master/misc/cpp/imgui_stdlib.cpp#L21 |
EDIT: Renamed to misc/cpp/imgui_stdlib.h
Just a little news post to state that I have added this file
misc/stl/imgui_stl.h
in the repo, which is an example of how to wire a std::string-like type to InputText() with the newImGuiInputTextFlags_CallbackResize
option.The two functions exposed in imgui_stl.h are currently:
Also see #2006 for details, but basically a simple wrapper for std:string would look like:
Feedback greatly welcome. Let me know if either the imgui_stl helper of the new
ImGuiInputTextFlags_CallbackResize
flag works for you.The text was updated successfully, but these errors were encountered: