Reputation: 1042
I have overloaded the stream insertion operator like this :
template<class Ch, class Tr, class word_type>
std::basic_ostream<Ch, Tr>&
operator << (std::basic_ostream<Ch, Tr>& s, const Mabit::mabit<word_type>& obj)
{
s << obj.to_string(Mabit::DEC, ',');
return s;
}
(mabit being the class for which i wanted the overload to work)
That is, since i can give different argument to the to_string
method, i would like to be able to use standard stream modifier like std::dec
, std::hex
... in a way that i could retrieve them from the overloaded operator to prepare the good argument as parameter for to_string
If i could also get the locale being used (to extract the separator for thousands) , it would be helpfull for the second argument too...
Is that possible ?
Upvotes: 3
Views: 256
Reputation: 1281
From http://www.cplusplus.com/reference/locale/numpunct/thousands_sep/
#include <iostream>
#include <locale>
using namespace std;
int main ()
{
int q=10977;
char separator = use_facet<numpunct<char> >(cout.getloc()).thousands_sep ();
cout << q/1000 << separator << q%1000 << endl;
return 0;
}
I suppose you can just replace cout with your stream argument in this example
Upvotes: 1
Reputation: 787
You can use std::basic_ostream::flags()
to identify whether an format specifier was used.
http://en.cppreference.com/w/cpp/io/ios_base/flags
Upvotes: 5