Solved

Modifying a facet object

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

776 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