Solved

Modifying a facet object

Posted on 2008-10-15
3
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.

756 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