
--- 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 } I'm missing surely something. Recall, I'm a beginner. How do you ensure
Le 10/11/11 20:44, Artyom Beilis a écrit : that any stream has associated a locale that contains your facet?
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.
How do you reach to reuse them. For example if we use a string stream we will need to add the facet. I guess that this operation will not take too much time, and in any case I expect it to load a dictionary that has already been loaded. So I suspect that the facet should only contain a key identifying the dictionary or a reference to it. Vicente