Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

3
  • I wonder if C_LOCALE = duplocale(LC_GLOBAL_LOCALE); would work? This assumes the global locale is valid (and equal to the C locale) before the first call to setlocale(), even though the POSIX spec says "If the locobj argument is LC_GLOBAL_LOCALE, duplocale() shall create a new locale object containing a copy of the global locale determined by the setlocale() function." Commented Aug 26 at 15:59
  • 1
    @IanAbbott I guess it should work but I don't see what it would give you unless there is reason to believe that newlocate(…, "C", …) is more expensive. The potential downside I see is that there is a risk that you get the order wrong; maybe a global constructor called setlocale before you could access it. However, duplicating followed by newlocale(…, base) (which, confusingly, modifies the base in-place) would be good to override only parts of the locale, e.g. the numeric mask while keeping other parts set to the current locale Commented Aug 26 at 22:27
  • This comes really close to my imagined answer of "just set it before the conversion and then revert to old locale after conversion", but since it's thread-specific, even better. Depending on performance requirements and/or overhead I'd suggest simply having a thread with the locale set that's only there for these conversions. Yes, it'd require some wrapper setup to have an easy-to-use function, so the set-convert-unset may be more practical Commented Aug 27 at 12:16