如何超载<<opeartor(double const&) in an inherited std::stringstream

问题描述 投票:0回答:1

我想覆盖

<<opeartor
这样

double d = 3.0;
mycustomstringstream << "Hello World " << d << "what a nice day.";
std::cout << mystream.str() << std::endl;

将产生监视器输出

Hello World (double)(3.00000000000)what a nice day.

我尝试了什么

压倒一切

因为我可以像这样打印双打,所以我大胆地实现了:

std::ostream& operator<<(std::ostream& o, double const& d){
  o<<"(double)("<< d << ")";
  return o;
}

这不起作用,因为编译器会解释歧义(因为该运算符已经定义)。

继承

就像一开始一样,我可以从 std::stringstream 继承,只需替换我的自定义字符串流的该运算符的定义即可:

#include<sstream>
class MyCustomStringStream: public std::stringstream{};

MyCustomStringStream& operator<<(MyCustomStringStream& o, double const& d){
  o<<"(double)("<< ( (std::stringstream)(o) << d ) << ")";
  return o;
}

这里的错误是:

error: use of deleted function 'std::__cxx11::basic_stringstream<_CharT, _Traits, _Alloc>::basic_stringstream(const std::__cxx11::basic_stringstream<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'

那么那么:

#include <iostream>
#include <sstream>

class MyStringStream: public std::stringstream{
    std::stringstream aux;
public:
    MyStringStream& operator<<(double const& d){
        aux.str() = "";
        aux << std::scientific << "((double)(" << d << ")";
        *this << aux.str();
        return *this;
    }
};

int main() {
    double d = 12.3;
    MyStringStream s;
    s << "Hello World " << d << "what a nice day.";
    std::cout << s.str() << std::endl;
}

但这仍然印着我

Hello World 12.3what a nice day.

演示[ https://godbolt.org/z/daG3bo6hv ]

c++ operator-overloading stringstream
1个回答
0
投票

无论你怎么想,继承通常都不是问题。无论如何,我不会从流派生(通常不建议从 STL 类型继承,尽管流允许这样做)。

对于您的情况,有一个更可重用的解决方案,制作一个辅助格式化对象:

#include <iostream>
#include <sstream>

template<typename type_t>
struct as_scientific
{
    type_t value;
};

template<typename type_t>
std::ostream& operator<<(std::ostream& os, const as_scientific<type_t>& scientific)
{
    os << std::scientific << "((" << typeid(type_t).name() << ")(" <<  scientific.value << ")";
    return os;
}

int main() 
{
    double d = 12.3;
    std::ostringstream s;
    s << "Hello World " << as_scientific{d} << "what a nice day.";
    std::cout << s.str() << "\n";

    // or directly
    std::cout << as_scientific{d} << "\n";
}
© www.soinside.com 2019 - 2024. All rights reserved.