partial template specialization for friend classes?
Asked Answered
I

2

12

I have a simple class X, and set of templatized classes Y<T,U>. I'd like all classes Y where the first templatization parameter happens to be X to be a friend of X itself. The following hopefully conveys what I want, but the friend statement gives a compile error.

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> friend class Y<X,U>;
};

I'm not sure templatization of friend statements is allowed at all (let alone partial templatization of friend statements). Is there a way to do this? Or am I stuck listing out all the friends one-by-one?

Thanks, Matt

Intellectualism answered 27/5, 2017 at 6:31 Comment(2)
"a compile error" ... please add it to your question.Cuesta
AFAIK, you can't. Friend declarations cannot refer to partial specializations, but can refer to full specializations.Jo
W
7

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

Wie answered 27/5, 2017 at 7:34 Comment(2)
Thanks for your response. I've opted for the non-partial approach you detailed. Though it makes more friends than needed, all classes Y are behind-the-scenes for what I'm doing, and so shouldn't be a big problem. Thanks for the syntax. I too tried your non-partial approach. Strangely it compiled, but didn't actually enable friendship as desired. Not sure what it's doing.Intellectualism
@fk0 The first part should work. If not, what compiler/arguments are you using? The second part has no effect, as I did write.Wie
M
15

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://mcmap.net/q/912423/-c-templates-partial-template-specifications-and-friend-classes

Messere answered 16/7, 2018 at 16:0 Comment(0)
W
7

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

Wie answered 27/5, 2017 at 7:34 Comment(2)
Thanks for your response. I've opted for the non-partial approach you detailed. Though it makes more friends than needed, all classes Y are behind-the-scenes for what I'm doing, and so shouldn't be a big problem. Thanks for the syntax. I too tried your non-partial approach. Strangely it compiled, but didn't actually enable friendship as desired. Not sure what it's doing.Intellectualism
@fk0 The first part should work. If not, what compiler/arguments are you using? The second part has no effect, as I did write.Wie

© 2022 - 2024 — McMap. All rights reserved.