简体   繁体   English

为类中定义的typedef定义ostream operator <<

[英]Defining ostream operator<< for typedef defined in the class

Could anyone provide me a hint how to implement correctly operator<< for MyType in provided code example? 任何人都可以提供一个提示如何在提供的代码示例中正确实现operator << for MyType?

#include <iostream>
#include <map>


template <typename T>
class A {
public:
    typedef std::map<unsigned int, T> MyType;
    MyType data;

    void show();
};

template <typename T>
std::ostream& operator<<(std::ostream& stream, typename A<T>::MyType const& mm)
{
    return stream << mm.size() << "\n";
}

//template <typename T>
//std::ostream& operator<<(std::ostream& stream, std::map<unsigned int, T> const& mm)
//{
//  return stream << mm.size() << "\n";
//}

template <typename T>
void A<T>::show() {std::cout << data;}

int main() {
    A<double> a;

    a.show();

    return 0;
}

Above code does not compile. 上面的代码不能编译。 But when I change definition of operator<< to commented out one, everything works correctly. 但是当我将operator <<的定义更改为注释掉时,一切都正常。 This is just a (not) working example of a more complicated problem and in reality MyType is much much more nasty. 这只是一个更复杂的问题的一个(非)工作示例,实际上MyType更加令人讨厌。 In that simple example I just could easily copy-paste exact definition of MyType from a 'A' class but in more complicated case, when this typedef is depending on antoher typedef... it would be nice just to refer to it. 在这个简单的例子中,我可以很容易地从'A'类中复制粘贴MyType的精确定义,但是在更复杂的情况下,当这个typedef依赖于antoher typedef时......引用它会很好。 Is there any solution to this problem? 有没有解决这个问题的方法?

Edit: 编辑:

Output error from compiler (in general as if operator<< was not defined at all, so when both definitions for operator<< from example are commented out compiler prints same error). 来自编译器的输出错误(通常好像操作符<<根本没有定义,因此当来自示例的operator <<的两个定义都被注释掉时,编译器会输出相同的错误)。

g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++14 -MMD -MP -MF"src/ostreamTest.d" -MT"src/ostreamTest.o" -o "src/ostreamTest.o" "../src/ostreamTest.cpp"
../src/ostreamTest.cpp:27:31: error: invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'MyType' (aka 'map<unsigned int, double>'))
    void A<T>::show() {std::cout << data;}
                       ~~~~~~~~~ ^  ~~~~

Problem is the non-deduced context (thanks PasserBy for the link), which disallows us to find a direct solution. 问题是未推断的上下文(感谢PasserBy的链接),这使我们无法找到直接的解决方案。

A workaround might be moving the typedef out of the class, such as this: 解决方法可能是将typedef移出类,例如:

template <typename T>
using A_MyType = std::map<unsigned int, T>;

template <typename T>
class A
{
public:
    typedef A_MyType<T> MyType;
    MyType data;

    void show();
};

template <typename T>
std::ostream& operator<<(std::ostream& stream, A_MyType<T> const& mm)
{
    return stream << mm.size() << std::endl;
}

Sure, this works fine for the std::map, if it works for your more complex class – impossible to say without knowing more details... 当然,这适用于std :: map,如果它适用于你更复杂的类 - 在不知道更多细节的情况下不可能说...

So I was cooking this as an answer 所以我正在做这个作为答案

#include <iostream>
#include <map>

template <typename T>
class A{
public:

    using MyType = std::map<unsigned int, T>;
    MyType data;

    void show();

    friend std::ostream& operator<<(std::ostream& stream, const MyType& mm){
        return stream << mm.size() << "\n";
    }
};

template <typename T>
void A<T>::show(){
    std::cout << data;
}

int main()
{
    A<double> a;
    std::cout << a;
    return 0;
}

But there's a build error quite known that occurs when compiling : 但是在编译时会出现一个众所周知的构建错误:
error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'

This error often means that the type deduction could not do its job properly (non-deductible context) (cf. Why is template type deduction not working and ostream lvalue error ) 此错误通常意味着类型推导无法正常工作(不可扣除上下文)(参见为什么模板类型推导不起作用ostream左值错误

Therefore I think that, for the moment, you should find another way around this problem, such way I can't think of for the moment. 因此,我认为,目前,你应该找到解决这个问题的另一种方法,这样我暂时想不到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM