template friends?

template friends?

Post by Dave » Fri, 01 Aug 2003 21:48:47



Question: How do you define a template friend?

I need to do this so I can define template functions which conversions from
other templated versions of the same class.  A listing is given below to
illustrate the problem....for the time being I have had to make all members
public which doesn't seem like a good solution.

I am guessing that template friends are not part of the C++ spec - if this
is the case are they planned for c++0x like template typedefs ?

Listing:
--------------------------------------------------
template <typename T>
struct A
{
   A() {}
   template <typename R>
   A(R &_rhs) { m_value = _rhs.m_value; } // this line will not compile since
m_value is private

   //friend template<typename R> class A<R>;
   // the above line does not compile

private:
   T m_value;

Quote:};

--------------------------------------------------

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

template friends?

Post by tom_usen » Sat, 02 Aug 2003 04:11:28



Quote:>Question: How do you define a template friend?

>I need to do this so I can define template functions which conversions from
>other templated versions of the same class.  A listing is given below to
>illustrate the problem....for the time being I have had to make all members
>public which doesn't seem like a good solution.

>I am guessing that template friends are not part of the C++ spec - if this
>is the case are they planned for c++0x like template typedefs ?

>Listing:
>--------------------------------------------------
>template <typename T>
>struct A
>{
>   A() {}
>   template <typename R>
>   A(R &_rhs) { m_value = _rhs.m_value; } // this line will not compile since
>m_value is private

>   //friend template<typename R> class A<R>;
>   // the above line does not compile

IIRC:
        template <typename R>
        friend class A;

Tom

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

template friends?

Post by Sebastian Molesk » Sat, 02 Aug 2003 09:36:51



Quote:> Question: How do you define a template friend?

You cannot. They are specifically forbidden by the standard.

sm

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

template friends?

Post by Rob Williscrof » Sat, 02 Aug 2003 09:38:28



Quote:> Question: How do you define a template friend?

> I need to do this so I can define template functions which conversions
> from other templated versions of the same class.  A listing is given
> below to illustrate the problem....for the time being I have had to
> make all members public which doesn't seem like a good solution.

> I am guessing that template friends are not part of the C++ spec - if
> this is the case are they planned for c++0x like template typedefs ?

> Listing:
> --------------------------------------------------
> template <typename T>
> struct A
> {
>    A() {}
>    template <typename R>
>    A(R &_rhs) { m_value = _rhs.m_value; } // this line will not
>    compile since
> m_value is private

>    //friend template<typename R> class A<R>;
>    // the above line does not compile

> private:
>    T m_value;
> };
> --------------------------------------------------

Your syntax was wrong, see below, I also made the template
constructor a bit less general.

#include <iostream>
#include <ostream>

template <typename T>
struct A
{
   A(T const &value = 0) { m_value = value; }
   template <typename R>
     A(A< R > &_rhs) { m_value = _rhs.m_value; }

   template <typename R> friend class A;

   void print() { std::cout << m_value << std::endl; }

private:
   T m_value;

Quote:};

int main()
{
  A< int > ai(2);
  A< double > ad( ai );

  ad.print();

Quote:}

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

template friends?

Post by David Lis » Sat, 02 Aug 2003 09:42:42


On 31 Jul 2003 08:48:47 -0400,


Quote:> Question: How do you define a template friend?

> I need to do this so I can define template functions which conversions from
> other templated versions of the same class.  A listing is given below to
> illustrate the problem....for the time being I have had to make all members
> public which doesn't seem like a good solution.

> I am guessing that template friends are not part of the C++ spec - if this
> is the case are they planned for c++0x like template typedefs ?

> Listing:
> --------------------------------------------------
> template <typename T>
> struct A
> {
>    A() {}
>    template <typename R>
>    A(R &_rhs) { m_value = _rhs.m_value; } // this line will not compile since
> m_value is private

>    //friend template<typename R> class A<R>;
>    // the above line does not compile

> private:
>    T m_value;
> };
> --------------------------------------------------

Is this possible for you to do?

Listing:
--------------------------------------------------
template <typename T, typename R>
struct A
{
public:
   A() {}
   explicit A(const R& _rhs)
     { m_value = _rhs.m_value; }
private:
   T m_value;
   friend class A<R>;

Quote:};

--------------------------------------------------

It may not be, depending on what you want to accomplish, but in many
situations it would be the obvious solution to your problem.

I'm not sure you want the A(const R&) constructor as an automatic
conversion constructor. I have declared it 'explicit' - and the
argument should of course be a const reference.

--
Med venlig hilsen / Best regards
David List

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

template friends?

Post by Richard Smit » Sat, 02 Aug 2003 09:52:15


Quote:> I am guessing that template friends are not part of the C++ spec - if this
> is the case are they planned for c++0x like template typedefs ?

Friend templates certainly are part of the C++98 Standard,
although some older compilers do not implement them.

Quote:> template <typename T>
> struct A
> {
>    A() {}
>    template <typename R>
>    A(R &_rhs) { m_value = _rhs.m_value; } // this line will not compile since
> m_value is private

Are you intending the function argument, R, to be another
instantiation of A<>?  If so, you might be better making
this explicit:

  template <typename R> A( A<R>& rhs ) { /* ... */ }

Second, it's often (though not always) best to make the
argument of the constructor a const reference.  ... But this
is straying from your original question.

Quote:>    //friend template<typename R> class A<R>;

This is nearly the correct syntax: there are two mistakes.
First the template<> and the friend are in the wrong order;
second you shouldn't repeat the <R> after the class name.
So the corrected version is

  template <typename R> friend class A;

Be aware, however, that some comparitively recent compliers
will not accept this code.

--
Richard Smith

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

template friends?

Post by John Potte » Sat, 02 Aug 2003 09:55:14



Quote:>    //friend template<typename R> class A<R>;
>    // the above line does not compile

template<typename R> friend class A;

John

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

template friends?

Post by Dhru » Sat, 02 Aug 2003 09:59:33



> Question: How do you define a template friend?

> I need to do this so I can define template functions which conversions from
> other templated versions of the same class.  A listing is given below to
> illustrate the problem....for the time being I have had to make all members
> public which doesn't seem like a good solution.

> I am guessing that template friends are not part of the C++ spec - if this
> is the case are they planned for c++0x like template typedefs ?

> Listing:
> --------------------------------------------------
> template <typename T>
> struct A
> {
>    A() {}
>    template <typename R>
>    A(R &_rhs) { m_value = _rhs.m_value; } // this line will not compile since
> m_value is private

>    //friend template<typename R> class A<R>;
>    // the above line does not compile

Should be:

template <class R>
freind class A;

Quote:

> private:
>    T m_value;

Regards,
-Dhruv.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

template friends?

Post by Stephan Bor » Sat, 02 Aug 2003 10:07:48


Dave schrieb:

Quote:> Question: How do you define a template friend?

> I need to do this so I can define template functions which conversions from
> other templated versions of the same class.  A listing is given below to
> illustrate the problem....for the time being I have had to make all members
> public which doesn't seem like a good solution.

> I am guessing that template friends are not part of the C++ spec - if this
> is the case are they planned for c++0x like template typedefs ?

Hello, I was interested in this question, too. And this is an elaborate
answer ( I think ).

http://www.cuj.com/documents/s=8244/cujcexp2101sutter/

Regards, Stephan Born
--
---------------------------------------------------------------
Dipl.-Inf. (FH) Stephan Born        | BRAIN FORCE SOFTWARE GmbH
                                     | Landsberger Allee 366
eMail: delete the capital letters   | 12681 Berlin
eMail: entferne die Grossbuchstaben | Germany
---------------------------------------------------------------
          PGP-Key verfgbar          |    PGP-Key available
---------------------------------------------------------------

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

template friends?

Post by Carl Danie » Sat, 02 Aug 2003 17:40:50


 > Question: How do you define a template friend?

template <typename T>
struct A
{
    A() {}
    template <typename R>
    A(const A<R> &_rhs) { m_value = _rhs.m_value; }

   template <class R> friend class A;

private:
    T m_value;

Quote:};

void foo()
{
    A<int> ai;
    A<float> af(ai);

Quote:}

-cd

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

template friends?

Post by llewell » Sun, 03 Aug 2003 01:14:07





>> Question: How do you define a template friend?

> You cannot. They are specifically forbidden by the standard.

a template cannot be a friend, but friend template is allowed; that
    is, one may write a template which instantiates as a friend
    declaration.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

template friends?

Post by Siemel Nara » Sun, 03 Aug 2003 02:15:15



> template<typename R> friend class A;

How does the compiler know which A you are talking about?  Is it A<R> or
A<T>?  Should we say
template<typename R> friend class A<R>;

--
+++++++++++
Siemel Naran

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

template friends?

Post by Richard Smit » Sun, 03 Aug 2003 17:41:23


 > > Question: How do you define a template friend?
 >
 > You cannot. They are specifically forbidden by the standard.

Perhaps you'd care to quote the bit of the standard that
specifically forbids them?

--
Richard Smith

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

template friends?

Post by John Potte » Sun, 03 Aug 2003 17:56:06


On 1 Aug 2003 13:15:15 -0400, "Siemel Naran"


 >
 > > template<typename R> friend class A;
 >
 > How does the compiler know which A you are talking about?  Is it A<R> or
 > A<T>?

A placeholder by any other name would smell the same.

 > Should we say
 > template<typename R> friend class A<R>;

Only if you desire a syntax error.  Try it.  Just like a forward
declaration, it says that a templated class is a friend.  It would
likely be better to remove the redundancy.

    template<class> friend class A;

John

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

template friends?

Post by Sebastian Molesk » Mon, 04 Aug 2003 05:04:49




>  > > Question: How do you define a template friend?

>  > You cannot. They are specifically forbidden by the standard.

> Perhaps you'd care to quote the bit of the standard that
> specifically forbids them?

As another poster pointed out before, I'm mistaken. Template friends are indeed
allowed and described in 14.5.3. I think what I got confused with were
constructs like

template<class T> friend class T;

which IIRC aren't allowed.

sm

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

1. friend templates and template friends

Hi,
        I have been looking everywhere, and can't find a solution to
this: How should a class template be declared as a friend?
1. template <class T> friend class A<T>;
2. friend template <class T>  class A<T>;

None of them work in gcc 2.8.0, which I have been using.

JJ

--
JJ Merelo                         | http://kal-el.ugr.es/htbin/jj-plex
Grupo Geneura ---- Univ. Granada  | http://kal-el.ugr.es/


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. LCD from Radio Shack

3. template friend function of a template class

4. Iges to AutoCAD

5. Template friends in class templates.

6. Meter Reading via Phone Line (was Re: Silent Night)

7. Template friends of template classes

8. Stream object and pumping data to the Response Stream

9. template friends

10. Q: template friend support in VC++ 7 (.NET)

11. non-template-friend

12. template friends

13. Templates, friend classes and namespace trouble