C++重载

标签 c++ visual-studio-2010 visual-c++ stream

下面的代码给我一个编译错误。谁能告诉我为什么?

class mytype {
public:
    int value;
    mytype(int a) {
        value = a;
    }
    friend ostream& operator<<(ostream& stream, const mytype& a) {
        stream << a.value;//works
        return stream;
    }
    friend ostringstream& operator<<(ostringstream& stream, const mytype& a) {
        stream << (a.value);//compilation error
        return stream;
    }
};

错误:

error C2027: use of undefined type 'std::basic_ostringstream<_Elem,_Traits,_Alloc>'

修复后:

error C2666: 'operator <<' : 18 overloads have similar conversions

最终修复:

Declare constructor as explicit. Works on MSVC then.

我想知道为什么。

最佳答案

error C2027: use of undefined type 'std::basic_ostringstream<_Elem,_Traits,_Alloc>'

你需要#include <sstream>得到[i/o]stringstream类。

关于其他错误

表单重载的问题

ostringstream& operator<<(ostringstream& stream, const mytype& a)

是它匹配 ostringstream 正是。为什么更精确的过载是一件坏事?根据标准,§13.3.3/1-2:

a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2) …

If there is exactly one viable function that is a better function than all other viable functions, then it is the one selected by overload resolution; otherwise the call is ill-formed

因此如果operator<<( ostream &, int )operator<<( ostringstream &, mytype const & )都是stream << value的候选人, 前者匹配 int完全没有转换,但后者匹配 ostringstream确切地。因此,对于所有论点来说,两者都“不差”,并且都不会被选中。

但是由于存在漏洞,代码有效的。你的重载根本不是候选者,除非你在函数调用中实际使用你的类型。当您声明/定义一个 friend在类 block 内,它不会将其引入任何命名空间范围;它只是它与类作用域相关联,这允许在该类类型描述传递的参数之一时找到它。

该标准对友元声明有这样的说法,尽管它在另一节 (14.6.5) 中:

Friend declarations do not introduce new names into any scope…

因此,MSVC 试图变得友善并主动将您的 friend 介绍给它的封闭命名空间。打击 MSVC。

然而,当我试图添加一个声明等同于 MSVC 所做的“免费”时,Comeau 和 GCC 很好地解决了由此产生的重载冲突——反对他们。或者是吗?事实证明,重载调用在文件中发生的时间早于我推荐的声明。如果我在 class mytype { 之前移动声明(这需要前向声明 mytype ),然后双方都适本地提示歧义。

根据标准的 §3.3-3.4,在命名空间范围内声明之前使用重载似乎很好。所以实际上 GCC 和 Comeau 都是对的。声明点紧跟在声明对象的名称之后。 (最后我检查过,自引用函数声明可以 still crash GCC 。)ADL 在封闭类之前的某个点调用对封闭命名空间的非限定查找。 (3.4.1/8 final bullet,3.4.1/9,3.4.2/2a。)如果 friend 在课前没有被宣布,那么它就不是候选人。 (7.3.1.2/3)C++是不是一门美丽的语言?

如何在 GCC 上保留简化示例,但破坏后续代码。

    friend ostringstream& operator<<(ostringstream& stream, const mytype& a) {
        stream << (a.value);//compilation error
        return stream;
    }
};

ostringstream& operator<<(ostringstream& stream, const mytype& a); // <- here

在此声明之后,将不可能编写 int进入 ostringstream .

如何使用更简单的声明语义统一打破一切。

class mytype; // <- here
              // and here:
inline ostringstream& operator<<(ostringstream& stream, const mytype& a);

class mytype {
public:

在此声明之后,将不可能编写 int进入 ostringstream ...包括class mytype {}中的 friend 声明.

实际解决方案。

流类应该是不可区分的。如果你真的想确定一个给定的流是否在内存中提供一个字符串(你不应该),最好查看它的内部 streambuf对象,由 rdbuf() 返回,它实际上执行 I/O 繁重的工作。即使是通用的 ostream对象可以有 ostringstream如果给出 stringbuf 的功能.

 if ( typeid( stream.rdbuf() ) == typeid( stringbuf * ) ) {
     // this is effectively a stringstream
 } else {
     // not a stringstream
 }

关于C++重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3898757/

相关文章:

c++ - 在编码程序中使用自己的堆栈类 - 确定大小和顶部

visual-studio - 从 Visual Studio 包 (VSIX) 如何检测解决方案或项目构建?

visual-studio-2010 - 变量 'xyz' 周围的堆栈已损坏

c++ - C++ 中的 new Bytef[int] 是什么?

c++ - cmake 链接 xlib 目录 C++

c# - 查找未处理的异常 - .NET

c++ - T() 应该将成员变量初始化为零吗?

c++ - 如何重载 << 运算符?

c++ - 错误 C2059 : syntax error : '<end Parse>'

C++共享库导出函数