gpt4 book ai didi

c++ - 自定义 std::ostream 子类中 operator<< 重载的歧义错误

转载 作者:行者123 更新时间:2023-11-30 04:43:03 24 4
gpt4 key购买 nike

在尝试开发一个继承自 std::stringbuf 的类时和 std::ostream并给它一个自定义 operator<<()重载(应该适用于此类,但一般不适用于 std::ostream),我遇到了一些我不理解的歧义错误。

问题出在下面代码的下半部分。如果我删除 operator<<()过载,我没有收到任何错误(g++ --std=c++17 -o foo.exe foo.cpp 使用 g++ 9.2.0,MSYS2 在 Windows 上构建)。

#include <iostream>
#include <ostream>
#include <sstream>

class MyStream:
private std::stringbuf,
public std::ostream
{
public:
MyStream() noexcept:
std::stringbuf(),
std::ostream(this)
{}

~MyStream()
{
if (pptr() - pbase() < 2)
{ sync(); }
}

protected:
virtual int sync() override
{
// make sure there actually is something to sync
if (pptr() - pbase() < 2)
{ return 0; }

const std::string message{str()};
const std::size_t length{message.length()};

// update the pointers for the next sync
setp(pbase() + length, epptr());

std::cout << message;

return std::stringbuf::sync();
}
};

MyStream& operator<<(MyStream& out, bool boolean) noexcept
{ return out << (boolean ? "yes" : "no"); }
/* ↑
more than one operator "<<" matches these operands:
-- function template "std::basic_ostream<_Elem, _Traits>
&std::operator<<(std::basic_ostream<_Elem, _Traits> &_Ostr, const char *_Val)"
-- function template "std::basic_ostream<char, _Traits>
&std::operator<<(std::basic_ostream<char, _Traits> &_Ostr, const char *_Val)"
-- function template "std::basic_ostream<_Elem, _Traits>
&std::operator<<(std::basic_ostream<_Elem, _Traits> &_Ostr, const _Elem *_Val)"
-- function "operator<<(MyStream &out, bool boolean) noexcept"
-- operand types are: MyStream << const char *
*/

int main()
{
MyStream stream;

stream << "Hello World" << std::endl;
/* ↑
more than one operator "<<" matches these operands:
-- function template "std::basic_ostream<_Elem, _Traits>
&std::operator<<(std::basic_ostream<_Elem, _Traits> &_Ostr, const char *_Val)"
-- function template "std::basic_ostream<char, _Traits>
&std::operator<<(std::basic_ostream<char, _Traits> &_Ostr, const char *_Val)"
-- function template "std::basic_ostream<_Elem, _Traits>
&std::operator<<(std::basic_ostream<_Elem, _Traits> &_Ostr, const _Elem *_Val)"
-- function "operator<<(MyStream &out, bool boolean) noexcept"
-- operand types are: MyStream << const char [12]
*/
}

为什么我的 bool 过载了? char* 的有效候选人争论?我怎样才能正确地做到这一点?

最佳答案

This似乎在 linux (g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0) 下对我有用:

MyStream& operator<<(MyStream& out, bool boolean)
{
out << (boolean ? "yes" : "no");
return out;
}

我只更改了返回调用。那是因为 out << (boolean ? "yes" : "no")将使用基础 ostream operator<<对于 char*类型,因此返回 std::basic_ostream<char>输入而不是 MyStream返回值类型。

然后在main()使用 bool输入 MyStream operator<<作品:

int main()
{
MyStream stream;
bool b=true;

stream << b << "Hello World" << b << std::endl;

//by contrast, this should output "yesHello Worldyes"
//stream << b << "Hello World";
//stream << b << std::endl;

//and so would this kind of ugliness! lol
//static_cast<MyStream&>(stream << b << "Hello World") << b << std::endl;
}

..但是一旦基类ostream operator<<"Hello World" 调用声明的一部分,它当然会返回一个 std::basic_ostream<char>输入 :)

..然后在那之后,因为不存在 ostream operator<<对于 bool类型,bool b类型将隐式提升为 int所以输出是:

yesHello World1 !


Windows 构建

但是您似乎遇到了其他错误。因此,虽然我手边没有 MSYS2,但我确实尝试使用 Visual Studio 在 WinOS 上编译代码。就我而言,至少,你的 main()由于 VS STL 实现引起的歧义,立即失败:

int main()
{
MyStream stream;
stream << "Hello World" << std::endl;
}

无需怀疑您当前的方法,因为评论中已经出现了这种情况。但只是为了回答您的问题;那么一种方法是实现另一个 operator<<对于 MyStream处理 char*类型:

#include <iostream>
#include <ostream>
#include <sstream>

class MyStream:
private std::stringbuf,
public std::ostream
{
public:
MyStream() noexcept:
std::stringbuf(),
std::ostream(this)
{}

~MyStream()
{
if (pptr() - pbase() < 2)
{ sync(); }
}

protected:
virtual int sync() override
{
// make sure there actually is something to sync
if (pptr() - pbase() < 2)
{ return 0; }

const std::string message{str()};
const std::size_t length{message.length()};

// update the pointers for the next sync
setp(pbase() + length, epptr());

std::cout << message;

return std::stringbuf::sync();
}
};

MyStream& operator<<(MyStream& out, const char* str)
{
static_cast<std::ostream&>(out) << str;
return out;
}

MyStream& operator<<(MyStream& out, bool boolean) noexcept
{ return out << (boolean ? "yes" : "no"); }


int main()
{
MyStream stream;
bool b=1;
stream << b << " oook " << b << std::endl;
}



或者,由于 MyStream实际上源自ostream然后我们可以使用任何 operator<<对于 ostream带有显式强制转换的类型。例如:

#include <iostream>
#include <ostream>
#include <sstream>

class MyStream:
private std::stringbuf,
public std::ostream
{
public:
MyStream() noexcept:
std::stringbuf(),
std::ostream(this)
{}

~MyStream()
{
if (pptr() - pbase() < 2)
{ sync(); }
}

protected:
virtual int sync() override
{
// make sure there actually is something to sync
if (pptr() - pbase() < 2)
{ return 0; }

const std::string message{str()};
const std::size_t length{message.length()};

// update the pointers for the next sync
setp(pbase() + length, epptr());

std::cout << message;

return std::stringbuf::sync();
}
};

std::ostream& operator<<(MyStream& out, bool boolean) noexcept
{
return static_cast<std::ostream&>(out) << (boolean ? "yes" : "no");
}

int main()
{
MyStream stream;
bool b=1;

stream << b << " ooOok ";
stream << b << std::endl;
}

希望输出的位置:

yes ooOok yes

关于c++ - 自定义 std::ostream 子类中 operator<< 重载的歧义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58486557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com