operator<<(ostream&, X) for class X nested in a class template

后端 未结 4 1413
终归单人心
终归单人心 2021-01-04 02:52

This one compiles and works like it should (non-nested template):

#include 

template class Z;

template          


        
4条回答
  •  情歌与酒
    2021-01-04 03:15

    Apart from the problem that the friend declaration doesn't match the operator template (perhaps fixable as)

    class ZZ {
        template
        friend std::ostream& operator<<(std::ostream& os, const ZZ&);
    };
    

    you also have a problem with a "non-deduced context", which is what Matthieu links to.

    In this template

    template 
    std::ostream& operator<< (std::ostream& os, const typename Z::ZZ&) {
        return (os << "ZZ!");
    }
    

    the compiler isn't able to figure out for what T's you parameter will match. There could be several matches, if you specialize for some types

    template<>
    class Z
    {
    public:
        typedef double   ZZ;
    };
    
    template<>
    class Z
    {
     public:
        typedef double   ZZ;
    };
    

    Now if I try to print a double, T could be either bool or long.

    The compiler cannot know this for sure without checking for all possible T's, and it doesn't have to do that. It just skips your operator instead.

提交回复
热议问题