-
Notifications
You must be signed in to change notification settings - Fork 117
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
Add vstrtonum util as replacement for atof/strtod, etc. #19309
base: develop
Are you sure you want to change the base?
Conversation
template<typename T> inline T _vstrtonum(char const *numstr, char **eptr, int /* unused */) { return static_cast<T>(strtold(numstr, eptr)); } | ||
|
||
// Specialize int/long cases to use int conversion strtol which with base of 0 can handle octal and hex also | ||
#define _VSTRTONUMI(T,F) template<> inline T _vstrtonum<T>(char const *numstr, char **eptr, int base) { return static_cast<T>(F(numstr, eptr, base)); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of macros, we can use std::function
along with the template to provide a generic way to forward and apply for each type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the macro is being used quite the way your comment suggests. Consumers of this interface use only the vstrtonum
templatized function...never a macro.
strtold()
would work for everything if we didn't care about handling bases other than 10 in conversions (e.g. octal or hex) and we would not need the specializations for integer data which include a base
argument.
This macro here is simply being used to easily instantiate several specializations for both signed and unsigned integer data to also support conversions involving bases other than 10. The specialization for unsigned cases is added error detection for passing negative values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, why we don't just due a normal instantiation? Templates should be the tool, not sure why we need both templates + macros?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what the issue is with using macros.
That said, my reason for using them is that it makes a) for a lot less typing and reading and, in particular, b) reading a lot of text that is substantially similar and trying to identify what the key difference is between them.
IMHO, macros make it very clear that the only difference in the three instantiations is the type and function called to perform the conversion.
It occurs to me...maybe we should be thinking about unicode string data as well? |
Description
This is a draft PR for developers to see where I am headed with this. It defines
vstrtonum<sometype>()
with some special sauce to handle things like default values, error checking, range checking, etc. Read the comment inStringHelpers.h
for an overview...visit/src/common/utility/StringHelpers.h
Lines 93 to 160 in dc5eefb