0

Class B uses a use a template, which is a private member of class A.

I tried friend declaration but the template is still private within the context.

How to allow B to use a private template of A?

Test (also at godbolt.org):

#include <iostream>
#include <vector>

template <class T>
class A {
private:
    template<typename R> using V = std::vector<R>;
};

template <typename T, class Prev>
class B {
public:
    friend Prev;
    typename Prev::template V<T> v_;
};

int main() {
    B<int, A<int>> b;
    return 0;
}

Compilation Error:

error: ‘template<class R> using V 
       = std::vector<R, std::allocator<_CharT> >’ 
is private within this context
     typename Prev::template V<T> v_;
                                  ^~
R zu
  • 2,034
  • 12
  • 30
  • 4
    `friend Prev;` makes `Prev` a friend of `B`. Not the other way round. The friendship has to be granted by `A`. – Bo Persson May 11 '18 at 16:29
  • Thanks. Will make that one public. Need coffee ... Ok, actually there is a better fix. Nevermind. – R zu May 11 '18 at 16:33

1 Answers1

0

Fix (also at godbolt.org)

#include <iostream>
#include <vector>

template <typename T, class Prev> class B;

template <class T>
class A {
    template <typename T2, class Prev> 
    friend class B;
private:
    template<typename R> using V = std::vector<R>;

};

template <typename T, class Prev>
class B {
public:
    typename Prev::template V<T> v_;
};

int main() {
    B<int, A<int>> b;
    return 0;
}
R zu
  • 2,034
  • 12
  • 30