If you are willing to change all of your include statements to your own internal header mystringstream.h
, you can use template specialization to pull this off, but with so many caveats I would not do it.
- You must be sure to use this header everywhere you would have included
sstream
previously.
- Your STL implementation must not have already specialized
basic_stringstream <char, char_traits<char>, allocator<char> >
- Your STL implementation, or any other header you include, must not have already instantiated stringstream
That being said, it worked in this simple codepad example.
// mystringstream.h
namespace std
{
// This class exists solely to "trick" the compiler into
// considering this allocator a new, different type
class newallocator : public allocator<char>
{
};
// template specialize std::stringstream to inherit from basic_stringstream
// using our newallocator in place of std::allocator<char>
template <>
class basic_stringstream<char, char_traits<char>, allocator<char> >
: public basic_stringstream <char, char_traits<char>, newallocator >
{
public:
basic_stringstream()
{
precision(16); // or whatever precision you like
}
};
}
I don't personally like this solution because it essentially modifies the behavior of the standard library, instead of extending it.