problem with a function template returning a private nested class ofa class template

Y

ymost

Hi,
I have a class template with two template arguments. It contains a
private nested class, and I want a non-member function to return an
instance of the nested class:

//-----------------------------------------------------------------
template<typename T1,typename T2> class A {
class B {};
};

template<typename T> typename A<T,T>::B func(T x){};

int main() {
A<int,int> aii;
A<double,double> add;
A<int,double> aid;
A<double,int> adi;
func(1);
return 0;
}
//----------------------------------------------------------------

(I created some instances of A with different types for testing).
Naturally I'm getting a 'class a<int, int>::B is private' error, but I
simply cannot succeed in making func a friend function of the class
template A. Each reference to func inside the class template A
(whether it's a friend declaration or the whole function definition)
causes either multiple ambiguous declarations of func, or an access
violation because the different instances of A are not friends of each
other (at least I think that's the reason).
Help will be appreciated.
Thanks
 
J

jason.cipriani

Hi,
I have a class template with two template arguments. It contains a
private nested class, and I want a non-member function to return an
instance of the nested class:

//-----------------------------------------------------------------
template<typename T1,typename T2> class A {
  class B {};

};

template<typename T> typename A<T,T>::B func(T x){};

int main() {
  A<int,int> aii;
  A<double,double> add;
  A<int,double> aid;
  A<double,int> adi;
  func(1);
  return 0;}

//----------------------------------------------------------------

(I created some instances of A with different types for testing).
Naturally I'm getting a 'class a<int, int>::B is private' error, but I
simply cannot succeed in making func a friend function of the class
template A. Each reference to func inside the class template A
(whether it's a friend declaration or the whole function definition)
causes either multiple ambiguous declarations of func, or an access
violation because the different instances of A are not friends of each
other (at least I think that's the reason).
Help will be appreciated.


You have 3 options:

1. Make the class public.

2. Make the function a member function.

3. Declare it as a friend, the syntax is:

template<typename T1,typename T2> class A {
class B {};
template<typename T> friend typename A<T,T>::B func(T x);
};

Note that that declares func<int> to be a friend of A<float,double>,
it's not completely strict about the types.

As for your access violation, that's not related to "friend". "Friend"
tells the compiler not to fail if the friend accesses a private/
protected member of a class, but it's inconsequential to accessing
memory outside of your program's boundaries.

Jason
 
Y

ymost

You have 3 options:

1. Make the class public.

2. Make the function a member function.

3. Declare it as a friend, the syntax is:

  template<typename T1,typename T2> class A {
    class B {};
    template<typename T> friend typename A<T,T>::B func(T x);
  };

Note that that declares func<int> to be a friend of A<float,double>,
it's not completely strict about the types.

As for your access violation, that's not related to "friend". "Friend"
tells the compiler not to fail if the friend accesses a private/
protected member of a class, but it's inconsequential to accessing
memory outside of your program's boundaries.

Jason

This solves my problem, thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top