partial template specialization for friend classes?

回眸只為那壹抹淺笑 提交于 2019-12-01 17:57:34

For the non-partial part of your question, the syntax is:

class X {
    template<class T, class U> friend class Y;
};

I guess, in most cases that should be sufficient.


With C++11 you can actually friend a templated alias:

template<typename T, typename U>
class Y { };

class X {
    public:
        X(int value) : i(value) {}
        const int& getI()    const { return i; }
    private:
        int    i;
        template<class U> using YX = Y<X,U>;
        template<class U> friend class YX;
};

However, that does not seem to work (I'm not sure if the friend declaration above has any effect at all).

The friend declaration page on cppreference.com specifies:

Friend declarations cannot refer to partial specializations, but can refer to full specializations

So as chtz said you can have a non-partial specialization friend.

Edit:

See also another answer on stackoverflow: https://stackoverflow.com/a/11046918/5776353

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!