We have a StreamBuffer
class in which we haven't implemented std::fixed
operations and I am trying to prevent number showing up in scientific notations. With my below code some numbers are getting shown in scientific notations. We want to avoid doing any allocations so that's why we implemented StreamBuffer
class because of performance reason.
Below is the code:
T value = 0;
template<typename U> void process(U& buf, DataOption holder) const {
if (holder == DataOption::TYPES) {
switch (type_) {
case teck::PROC_FLOAT:
buf << "{"float":" << value << "}";
break;
case teck::PROC_DOUBLE:
buf << "{"double":" << value << "}";
break;
default:
buf << "{"" << type_ << "":" << value << "}";
}
}
}
And this is the way it is being called:
void HolderProcess::dump(std::ostream& os, DataOption holder) const
{
process<std::ostream>(os, holder);
}
void HolderProcess::dump(StreamBuffer& buffer, DataOption holder) const
{
process<StreamBuffer>(buffer, holder);
}
I tried using like as shown below and I got an error by which I understood we cannot use std::fixed
on my StreamBuffer
class.
case teck::PROC_DOUBLE:
buf << "{"double":" << std::fixed << value << "}";
What is the alternative to std::fixed
I can use here which doesn't do any allocations at all. I was thinking of converting number to string and then apply std::fixed
on it but that will do some allocations as well which I want to avoid that.
What is the best way to do this which is performance efficient and doesn't do any allocations? I have a below solution but it will do some allocations as it uses string. I can call below method from my above code.
template <typename T> string str(T number)
{
std::ostringstream ss;
ss << std::fixed << number;
return ss.str();
}
Is there any other optimized and efficient way?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…