Extending numpunct by derivation

It is easy enough to derive from numpunct and override the virtual functions in a portable manner. But numpunct also has a non-standard protected interface that you can take advantage of if you wish. There are five protected data members:

  char_type __decimal_point_;
  char_type __thousands_sep_;
  string    __grouping_;
  string_type __truename_;
  string_type __falsename_;
  

A derived class could set these data members in its constructor to whatever is appropriate, and thus not need to override the virtual methods.

Listing: Example of numpunct<char>
struct mypunct: public std::numpunct<char>
{

   mypunct();

};

mypunct::mypunct()

{

   __decimal_point_ = ',';

   __thousands_sep_ = '.';

   __grouping_ = "\3\2";

   __falsename_ = "nope";

   __truename_ = "sure";

}

int main()

{

   std::locale loc(std::locale(), new mypunct);

   std::cout.imbue(loc);

   // ...

}