Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ get stream modifier from << overload

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 ?

like image 372
jav974 Avatar asked Dec 06 '25 14:12

jav974


1 Answers

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

like image 182
Steven Maitlall Avatar answered Dec 08 '25 03:12

Steven Maitlall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!