suppose we have template class X and template function
template <class A, class B> A* g (B&);
how to make something like that?
template <class T>
class X {
template <class A, class B>
friend B* g (T&);
};
i mean that g<A, B> () is only friend of X <B>?

> i mean that g<A, B> () is only friend of X <B>?

template < typename T1, typename T2 > T1* g( T2& ) ;

class A ;

template < typename T > class X
{
  friend A* g <> ( T& ) ;
  // ...
};

> i mean that g<A, B> () is only friend of X <B>?

template < typename T1, typename T2 > T1* g( T2& ) ;

class A ;

template < typename T > class X
{
  friend A* g <> ( T& ) ;
  // ...
};
template <class T1, class T2> T1* g (T2&);

class A;

template <class T> class X
{
private:
    T *ptr;
public:
    X (T *ptr) : ptr (ptr) {}

    friend A* g <> (T&);
};

template <class T1, class T2> T1* g (T2& x) {
    return (T1*) x.ptr;
}

...
X<int> klasa_x ((int*)1);
int* wsk_wyd = g<int, X<int> > (klasa_x);
printf ("%d\n", wsk_wyd);
...

this code based on your solution doesn't really work under GNU C++ compiler
It says that ptr is private variable

the friend function that you require for this has a different signature.

#include <iostream>
template < typename T1,  typename T2 > T1* g ( T2& ) ;

template < typename T >  class X
{
  private:
      T *ptr;
  public:
      X (T *ptr) : ptr (ptr) {}

      friend T* g <> ( X<T>& );
};

template < typename T1,  typename T2> T1* g (T2& x)
{  return x.ptr; }

int main()
{
  X<int> klasa_x ( reinterpret_cast<int*>(1) ) ;
  int* wsk_wyd = g< int, X<int> > ( klasa_x );
  std::cout << wsk_wyd << '\n' ;
}

you might find that writing the code this way would be more flexible in the long run.

#include <iostream>
template < typename T > typename T::pointer_type g ( T& ) ;

template < typename T >  class X
{
  private:
      T* ptr;
  public:
      typedef T* pointer_type ; 
      X (T *ptr) : ptr (ptr) {}

      friend pointer_type g <> ( X<T>& );
};

template < typename T > typename T::pointer_type g( T& object )
{  return object.ptr;  }

int main()
{
  X<int> klasa_x ( reinterpret_cast<int*>(1) ) ;
  int* wsk_wyd = g( klasa_x );
  std::cout << wsk_wyd << '\n' ;
}

but that's unfortunately not solution to my problem. I solved it somehow different, but i wonder if it is possible to do something like I asked at the beginning? It is possible with Microsoft compiler with the code in my first post. Maybe GNU C++ doesn't support it?
Ups, actually there was mistake in code from my first post. It should be:

template <class A, class B> A* g (B&);
template <class T>
class X {
template <class A, class B>
friend A* g (T&);
};

...Maybe GNU C++ doesn't support it?
Ups, actually there was mistake in code from my first post. It should be:

template <class A, class B> A* g (B&);
template <class T>
class X {
template <class A, class B>
friend A* g (T&);
};

the friend declaration in this snippet does not give the effect that you are seeking

( i mean that g<A, B> () is only friend of X <B> )

perhaps this example would clarify:

template <class A, class B> A* g (B&);
template <class A, class B> A* g (int&);

template <class T>
class X
{
  template <class A, class B>
    friend A* g (T&);
  private : T i ;
};

template <class A, class B> A* g (B& b)
{
  // when called with type B == X<int>
  return &b.i ; // error: not a friend of X<int>
}

template <class A, class B> A* g (int&)
{
  static B xx ;
  return &xx.i ; // ok, friend of X<int>
}

int main()
{
  X<int> xx ;
  // g< int, X<int> >(xx) ; // error: 'int X<int>::i' is private

  int i = 8 ;
  // ok, int* g< int, X<int> >(int&) is a friend of X<int>
  g< int, X<int> >(i) ; 
}

for some additional information see:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16
http://www.devx.com/cplus/10MinuteSolution/30302/0/page/3

for a more detailed discussion, see
C++ Templates: The Complete Guide by David Vandevoorde, Nicolai M. Josuttis isbn: 0-201-73484-2
sections 8.4, 9.1, 9.2 and 9.3

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.