c++ - Can a friend of A<T> be also a friend of A<A<T>>? -


consider 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 here main.cpp:8:20: error: 'std::vector<container<int>, std::allocator<container<int> > > container<container<int> >::v' private      std::vector<t> v;                     ^ main.cpp:20:9: error: within context          c.v.push_back(container<t>(x)); 

which believe correct, because make_double_container friend of container<t>, not of container<container<t>>. how can make make_double_container work in situation?

clearly, can make every specialization of make_double_container friend:

template <typename u> friend container<container<u>> make_double_container(const std::vector<std::vector<u>>&); 

if want keep friendship minimum without partial specialization or 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.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -