Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a friend of A<T> be also a friend of A<A<T>>?

Consider the following code:

#include <vector>

template<typename T> class Container;
template<typename T> Container<Container<T>> make_double_container(const std::vector<std::vector<T>>&);

template<typename T>
class Container {
    std::vector<T> v;
    friend Container<Container<T>> make_double_container<T>(const std::vector<std::vector<T>>&);

public:
    Container() {}
    explicit Container(std::vector<T> v) : v(v) {}
};

template<typename T>
Container<Container<T>> make_double_container(const std::vector<std::vector<T>>& v) {
    Container<Container<T>> c;
    for(const auto& x : v) {
        c.v.push_back(Container<T>(x));
    }
    return c;
}

int main() {
    std::vector<std::vector<int>> v{{1,2,3},{4,5,6}};
    auto c = make_double_container(v);
    return 0;
}

The compiler tells me that:

main.cpp: In instantiation of 'Container<Container<T> > make_double_container(const std::vector<std::vector<T> >&) [with T = int]':
main.cpp:27:37:   required from here
main.cpp:8:20: error: 'std::vector<Container<int>, std::allocator<Container<int> > > Container<Container<int> >::v' is private
     std::vector<T> v;
                    ^
main.cpp:20:9: error: within this context
         c.v.push_back(Container<T>(x));

Which I believe to be correct, because make_double_container is friend of Container<T>, but not of Container<Container<T>>. How can I make make_double_container work in this situation?

like image 983
Alberto Santini Avatar asked Nov 01 '15 16:11

Alberto Santini


People also ask

Can you have friends of the opposite gender in a relationship?

Not all opposite-sex friendships are dangerous, but it is important to err on the side of caution. It is helpful to discuss the nature of your friendship on a regular basis with your spouse. If not kept in check, a totally innocent relationship could end up causing unnecessary harm to your marriage.

Can a guy and a girl just be friends?

Sociologists have documented that men and women can indeed just be friends and that there are actually benefits that come with cross-sex friendships — like learning from the other side how to best attract a mate — that you can't get from same-sex friendships.

Can bosses and employees be friends?

When people trust and respect each other, just as in any healthy relationship, employee/manager friendships can build growth, enhance engagement, and make the workplace more productive.

What do you call a person with no friends?

(frɛndlɪs ) adjective. Someone who is friendless has no friends. The boy was unhappy because he thought he was friendless. Synonyms: alone, abandoned, deserted, isolated More Synonyms of friendless.


1 Answers

Clearly, you can make every specialization of make_double_container a friend:

template <typename U>
friend Container<Container<U>> make_double_container(const std::vector<std::vector<U>>&);

If you want to keep friendship to a minimum without partial specialization or the like, try

template <typename> struct extract {using type=void;};
template <typename U> struct extract<Container<U>> {using type=U;};
friend Container<Container<typename extract<T>::type>>
     make_double_container(const std::vector<std::vector<typename extract<T>::type>>&);

Demo.

like image 164
Columbo Avatar answered Oct 23 '22 10:10

Columbo