
--- On Thu, 11/10/11, Vicente Botet <vicente.botet@wanadoo.fr> wrote:
This makes sure that if somebody links with the
From: Vicente Botet <vicente.botet@wanadoo.fr> library the
static ids (facet's ids) and static variables allocated with xalloc are installed.
Oh, I see. Unfortunately this technique can not be applied when the library is header-only, isn't it?
Not... but you can use anonymous namespace and it would work but it is little big ugly.
But you can fallback to default if facet is not
installed, i.e.:
if(!have_facet<foo>(loc)) // Do something as std::locale::classic
dooes
else use_facet<foo>(loc).bar(...)
Same for configuration. Ugly but useful for high level functions.
Yes, this is a good technique so that imbuing is not done by the library, only by the user. However this technique doesn't solves the multi-threaded imbuing issue.
But I don't expect from user to change locale in a stream and use it in same time?! In any case you can do: try { foo const &f =use_facet<foo>(f); // do required } catch(std::bad_cast const &) { // do default }
For non ICU based facets, there is no such problem.
I guess we can define always immutable facets that reference a mutable storage. Facet should be lightweight so that the creation doesn't takes too much time.
No actually, it is not. facets are not generated frequently. locale is cheap to copy because facets are reference counted so you create facet once and use it for long time. For example messages facet can load dictionaries convert their encoding, it is by no means cheap. But you create it once and then reuse it. Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.sf.net/ CppDB - C++ SQL Connectivity: http://cppcms.sf.net/sql/cppdb/