Solved

Modifying a facet object

Posted on 2008-10-15
3
286 Views
Last Modified: 2012-05-05
It seems that the way the C++ standard library is set up discourages modifying an existing facet object that is associated with a locale, because the only way to access a facet object is through std::use_facet, and std::use_facet returns a const reference.

But suppose, for example. you have a custom facet object that regulates the output of something like dates.  And suppose the facet object has two internal flags to represent a date format: the American style of month, day, year, and the European style of day, month, year.  It would be very convenient to create a stream manipulator function which allows you to toggle between the two styles, so you could do something like:

Date d;
cout << euro_style << d << endl;

So the manipulator would be implemented as:

std::ostream& euro_style(std::ostream& os)
{
     if (std::has_facet<date_facet>(os.getloc())
          std::use_facet<date_facet>(os.getloc()).set_euro_style();
     return os;
}

Of course, the above code doesn't work because std::use_facet returns a constant reference, and date_facet::set_euro_style() is a non-const member function.  So in order to make it work, you need to use a const_cast, or use mutable internal variables in the facet object.  This is no problem, but it says to me that the designers of the C++ I/O stream library are trying to discourage modification of facet objects.  This makes me wonder if there is some reason why it is dangerous or unwise to modify a facet object that is already bound to a locale.  Otherwise, why does use_facet return a constant reference?
0
Comment
Question by:chsalvia
  • 2
3 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22719150
>> It would be very convenient to create a stream manipulator function which allows you to toggle between the two styles, so you could do something like:

Wouldn't you be overriding the locale in that case ? If you're in a US locale, then the format is by default the American format. No need to set it to European format.

I guess the reasoning is that the behavior of facets is only dependent on the locale, not on extra user settings (that might override the locale).
0
 

Author Comment

by:chsalvia
ID: 22719198
That's true, but the US/European date format was just an example I came up with.  In general, is there some reason why you shouldn't modify a facet object using std::use_facet?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 22719222
>> In general, is there some reason why you shouldn't modify a facet object using std::use_facet?

Well, as I said, I would assume the reasoning was that the behavior of facets is only dependent on the locale, not on extra user settings (that might override the locale). If facets need to be user configurable, then it might be a better idea to place that user configuration in a separate object. But that's just an idea.

In any case, I'd stay away from const_casts unless there's no other way (and that's very rare).
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question