Solved

Modifying a facet object

Posted on 2008-10-15
3
283 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now