[ create a new paste ] login | about

Link: http://codepad.org/rbMas8mg    [ raw code | output | fork ]

pjohnmeyer - C++, pasted on Mar 6:
#include <sstream>

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
    }
  };
}

int main()
{
  std::stringstream ss;
  double d = 0.1234567890123456;

  ss << d;

  std::cout << ss.str() << std::endl;
}


Output:
1
0.1234567890123456


Create a new paste based on this one


Comments: