Topic: templates and friends


Author: "John Hickin" <hickin@nortelnetworks.com>
Date: Mon, 30 Oct 2000 18:38:31 GMT
Raw View
news wrote:
>
> A colleague of mine encountered problems when compiling a piece of code,
> similar to the following fragment:
>
>     template<class T>
>     class M
>     {
>         friend class T;
>     public:
>         M() {}
>     };
>

>
> At my first, it does seem reasonable, standard compliant code, but perhaps
> I've missed something. Any help or information on this issue would be highly
> appreciated.
>

It is not legal. One might hope (at least I did) to introduce a typedef
to get around the problem, but that isn't legal either. Consider:

  template<typename T>
  struct B {
    typedef T ParameterType;
  };

  template<rtpename T>
  struct D : public B<T>
  {
    friend ParameterType;
    // not legal (and maybe incorrect syntax too)
    // but let's suppose it was legal
    ...
  };

Great, until somebody introduces, for example, a class ParameterType, at
global scope. Now D<T>'s friend has been hijacked.


Regards, John.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: joerg.barfurth@attglobal.net (Joerg Barfurth)
Date: 2000/10/22
Raw View
Roger Onslow <roger_onslow@dingoblue.net.au> wrote:

> "friend T" is probably what you want, rather than "friend class T".
>=20
> One would assume that T corresponds to a class already so it is a _bit_=
 like
> saying "friend class class T".

The Standard says in 11.4[Friends]/p.2:

<quote src=3D"ISO+IEC+14882-1998">
  <text status=3D"normative">
An /elaborated-type-specifier/ shall be used in a friend declaration for
a class.
  </text>
  <footnote nr=3D"101">
    The /class-key/ of the /elaborated-type-specifier/ is required.
  <footnote/>
</quote>

--=20
J=F6rg Barfurth                         joerg.barfurth@attglobal.net
-------------- using std::disclaimer; -----------------------------
Download:     StarOffice 5.2 at       http://www.sun.com/staroffice
Participate:  OpenOffice now at       http://www.OpenOffice.org

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Roger Onslow" <roger_onslow@dingoblue.net.au>
Date: 2000/10/23
Raw View
So you are saying that

class AlreadyDeclared
{
    ...
};

class HasFriends
{
public:
    friend AreadyDeclared;
};

is wrong because the word 'class' is ommitted from the friend declaration?
I don't think so.

Or are you simply agreeing with me that an elaborated type spec is NOT
allowed when the type is a template (which is what I was saying in layman's
terms).

--
Roger Onslow
Software Developer
See my articles at http://www.codeguru.com
See the product I am working on at http://www.swishzone.com

"Operator .. give me the number for 911" .. Homer Simpson

"Joerg Barfurth" <joerg.barfurth@attglobal.net> wrote in message
news:1eiu0bc.1qskurn1l95cunN%joerg.barfurth@attglobal.net...
Roger Onslow <roger_onslow@dingoblue.net.au> wrote:

> "friend T" is probably what you want, rather than "friend class T".
>
> One would assume that T corresponds to a class already so it is a _bit_
like
> saying "friend class class T".

The Standard says in 11.4[Friends]/p.2:

<quote src="ISO+IEC+14882-1998">
  <text status="normative">
An /elaborated-type-specifier/ shall be used in a friend declaration for
a class.
  </text>
  <footnote nr="101">
    The /class-key/ of the /elaborated-type-specifier/ is required.
  <footnote/>
</quote>

--
J   rg Barfurth                         joerg.barfurth@attglobal.net
-------------- using std::disclaimer; -----------------------------
Download:     StarOffice 5.2 at       http://www.sun.com/staroffice
Participate:  OpenOffice now at       http://www.OpenOffice.org

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: joerg.barfurth@attglobal.net (Joerg Barfurth)
Date: 2000/10/23
Raw View
Roger Onslow <roger_onslow@dingoblue.net.au> wrote:

> So you are saying that
>=20
> class AlreadyDeclared
> {
>     ...
> };
>=20
> class HasFriends
> {
> public:
>     friend AreadyDeclared;
> };
>=20
> is wrong because the word 'class' is ommitted from the friend declarati=
on?

Yes.

> I don't think so.

I do. A diagnostic is required otherwise.

> Or are you simply agreeing with me that an elaborated type spec is NOT
> allowed when the type is a template (which is what I was saying in laym=
an's
> terms).

No.


> The Standard says in 11.4[Friends]/p.2:
>=20
> <quote src=3D"ISO+IEC+14882-1998">
>   <text status=3D"normative">
> An /elaborated-type-specifier/ shall be used in a friend declaration fo=
r
> a class.
>   </text>
>   <footnote nr=3D"101">
>     The /class-key/ of the /elaborated-type-specifier/ is required.
>   <footnote/>
> </quote>

--
> J=F6rg Barfurth                         joerg.barfurth@attglobal.net
> -------------- using std::disclaimer; -----------------------------
> Download:     StarOffice 5.2 at       http://www.sun.com/staroffice
> Participate:  OpenOffice now at       http://www.OpenOffice.org
Save the environment - recycle your sigs ...

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Roger Onslow" <roger_onslow@dingoblue.net.au>
Date: 2000/10/24
Raw View
Hmm .. I've yet to see a compiler complain about this (mind you, I haven't
used a large number of them).

Not that it means much, but VC certainly quite happily compiles

class X { } ;
class Y { friend X; };

without any complaint.

Of course, just because a particular compiler (or even every compiler)
compiles some code without errors or warnings does not mean that it is
correct code .. just the every compiler that DOESN'T warn is non-standard
conforming.

Nevertheless, just out of curiosity, does anyone know of compiler(s) that do
complain about the friend statement above?  Or other compilers that do NOT
complain?

--
Roger Onslow
Software Developer
See my articles at http://www.codeguru.com
See the product I am working on at http://www.swishzone.com

"Operator .. give me the number for 911" .. Homer Simpson

"Joerg Barfurth" <joerg.barfurth@attglobal.net> wrote in message
news:1eizc15.wdygq51heqzcoN%joerg.barfurth@attglobal.net...
>    Roger Onslow <roger_onslow@dingoblue.net.au> wrote:
>> So you are saying that
>> class AlreadyDeclared
>> {
>>     ...
>> };
>> class HasFriends
>> {
>> public:
>>     friend AreadyDeclared;
>> };
>> is wrong because the word 'class' is ommitted from the friend
declaration?
>Yes.
>> I don't think so.
>I do. A diagnostic is required otherwise.
>> Or are you simply agreeing with me that an elaborated type spec is NOT
>> allowed when the type is a template (which is what I was saying in
layman's
>> terms).
>No.
>> The Standard says in 11.4[Friends]/p.2:
>>> An /elaborated-type-specifier/ shall be used in a friend declaration for
>>> a class.
>>> The /class-key/ of the /elaborated-type-specifier/ is required.



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: joerg.barfurth@attglobal.net (Joerg Barfurth)
Date: Wed, 25 Oct 2000 00:40:20 GMT
Raw View
Roger Onslow <roger_onslow@dingoblue.net.au> wrote:

> Hmm .. I've yet to see a compiler complain about this (mind you, I have=
n't
> used a large number of them).
>
> Not that it means much, but VC certainly quite happily compiles

Even in its most conforming mode ? I know that said mode is not too
useful, but it should be possible to compile this fragment in it.

> class X { } ;
> class Y { friend X; };

> Nevertheless, just out of curiosity, does anyone know of compiler(s) th=
at do
> complain about the friend statement above? =20

<compile-result href=3D"http://www.comeaucomputing.com/tryitout">
  Comeau C/C++ 4.2.44 (Oct  3 2000 17:18:45) for ONLINE_EVALUATION_BETA3
  Copyright 1988-2000 Comeau Computing.  All rights reserved.
  MODE:strict errors C++

  "2909.c", line 2: error: omission of "class" is nonstandard
    class Y { friend X; };
                   ^
  1 error detected in the compilation of "2909.c".
</compile-result>

Regards, J=F6rg

--=20
J=F6rg Barfurth                         joerg.barfurth@attglobal.net
-------------- using std::disclaimer; -----------------------------
Download:     StarOffice 5.2 at       http://www.sun.com/staroffice
Participate:  OpenOffice now at       http://www.OpenOffice.org

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: wmm@fastdial.net
Date: Mon, 30 Oct 2000 17:46:40 GMT
Raw View
In article <newscache$qlso2g$nw8$1@news.freegates.be>,
  news <news@onew2.UCAR.EDU> wrote:
> A colleague of mine encountered problems when compiling a piece of
code,
> similar to the following fragment:
>
>     template<class T>
>     class M
>     {
>         friend class T;
>     public:
>         M() {}
>     };
>
> This is the actual content of a reduced version of the source of the
real
> problem, but with the same result: it does not compile on the C++
compiler
> on VMS 7.2, giving the following error:
>
>     %CXX-W-TMPLPRMELABTYP, (1) template parameter T may not be used
in an
> elaborated type specifier
>
> It compiled without any complaits on the VC++ compiler (on Windows
NT).
>
> At my first, it does seem reasonable, standard compliant code, but
perhaps
> I've missed something. Any help or information on this issue would be
highly
> appreciated.

This is explicitly disallowed in 7.1.5.3p2:

    3.4.4 describes how name lookup proceeds for the identifier
    in an elaborated-type-specifier... If the identifier
    resolves to a typedef-name or a template type-parameter,
    the elaborated-type-specifier is ill-formed. [Note: this
    implies that, within a class template with a template
    type-parameter T, the declaration

        friend class T;

    is ill-formed. ]

--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: news <news@onew2.UCAR.EDU>
Date: 2000/10/19
Raw View
A colleague of mine encountered problems when compiling a piece of code,
similar to the following fragment:

    template<class T>
    class M
    {
        friend class T;
    public:
        M() {}
    };

This is the actual content of a reduced version of the source of the real
problem, but with the same result: it does not compile on the C++ compiler
on VMS 7.2, giving the following error:

    %CXX-W-TMPLPRMELABTYP, (1) template parameter T may not be used in an
elaborated type specifier

It compiled without any complaits on the VC++ compiler (on Windows NT).

At my first, it does seem reasonable, standard compliant code, but perhaps
I've missed something. Any help or information on this issue would be highly
appreciated.

Jean-Marc Talloen



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Roger Onslow" <roger_onslow@dingoblue.net.au>
Date: 2000/10/20
Raw View
"friend T" is probably what you want, rather than "friend class T".

One would assume that T corresponds to a class already so it is a _bit_ like
saying "friend class class T".
--
Roger Onslow
Software Developer
See my articles at http://www.codeguru.com
See the product I am working on at http://www.swishzone.com

"Operator .. give me the number for 911" .. Homer Simpson

"news" <news@onew2.UCAR.EDU> wrote in message
news:newscache$qlso2g$nw8$1@news.freegates.be...
> A colleague of mine encountered problems when compiling a piece of code,
> similar to the following fragment:
>
>     template<class T>
>     class M
>     {
>         friend class T;
>     public:
>         M() {}
>     };
>
> This is the actual content of a reduced version of the source of the real
> problem, but with the same result: it does not compile on the C++ compiler
> on VMS 7.2, giving the following error:
>
>     %CXX-W-TMPLPRMELABTYP, (1) template parameter T may not be used in an
> elaborated type specifier
>
> It compiled without any complaits on the VC++ compiler (on Windows NT).
>
> At my first, it does seem reasonable, standard compliant code, but perhaps
> I've missed something. Any help or information on this issue would be
highly
> appreciated.
>
> Jean-Marc Talloen
>
>
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
> [ Note that the FAQ URL has changed!  Please update your bookmarks.     ]
>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: joerg.barfurth@attglobal.net (Joerg Barfurth)
Date: 2000/10/22
Raw View
news <news@onew2.UCAR.EDU> wrote:

> A colleague of mine encountered problems when compiling a piece of code=
,
> similar to the following fragment:
>=20
>     template<class T>
>     class M
>     {
>         friend class T;
>     public:
>         M() {}
>     };
>=20
> This is the actual content of a reduced version of the source of the re=
al
> problem, but with the same result: it does not compile on the C++ compi=
ler
[...]
> At my first, it does seem reasonable, standard compliant code, but perh=
aps
> I've missed something. Any help or information on this issue would be h=
ighly
> appreciated.

In the Standard 11.4/2 says that a friend declaration for a class must
use an /elaborated-type-specifier/ (as well you did: "class T") with
some additional restrictions.

But then 7.1.5.3/2 states that the /identifier/ in the
/elaborated-type-specifier/ must not evaluate to a template parameter
name.

A footnote there pinpoints, that this renders your code illegal.

J=F6rg

--=20
J=F6rg Barfurth                         joerg.barfurth@attglobal.net
-------------- using std::disclaimer; -----------------------------
Download:     StarOffice 5.2 at       http://www.sun.com/staroffice
Participate:  OpenOffice now at       http://www.OpenOffice.org

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]