partial template specialization for friend classes?

后端 未结 2 1960
一整个雨季
一整个雨季 2021-01-18 05:40

I have a simple class X, and set of templatized classes Y. I\'d like all classes Y where the first templatization parameter happens to be X to be a friend of X i

相关标签:
2条回答
  • 2021-01-18 06:00

    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

    0 讨论(0)
  • 2021-01-18 06:03

    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).

    0 讨论(0)
提交回复
热议问题