Solved

Modifying a facet object

Posted on 2008-10-15
3
282 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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
>> 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

762 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

9 Experts available now in Live!

Get 1:1 Help Now