Topic: Parameterized friend classes of template classes


Author: James Kuyper <kuyper@wizard.net>
Date: 1999/11/05
Raw View
TiTi wrote:
...
> specialization, a new friend would be born. Since C++ compiler/linker is not
> a maternity (don't really know the word for that), it refuses to participate

I think that "maternity ward" is the term you're looking for.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Gene Bushuyev" <gbush@my-deja.com>
Date: 1999/11/04
Raw View
TiTi <TJunkMeBigTime@mad.dog.com> wrote in message
news:7vn02v$9hu$1@trex.antw.online.be...
[snip]
> Just think of such a specialization for the 'int' class. There is no way
> that the 'int' class can become a friend of the template class.
[snip]

So what? There are many cases when a template can be instantiated only on
certain types and not on all types. Why does this case makes difference?
The only problem that exists is that the same name can be present in the
outer scope. It could be resolved of course. The lookup could be postponed
to the time of instantiation of a template.
The truth is that the concept of templates is not thoroughly developed in
the current release of the standard. The functionality that we are looking
for is not something impossible or inherently contradictory.

Gene Bushuyev




[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/11/04
Raw View
"TiTi" <TJunkMeBigTime@mad.dog.com> writes:
> Just think of such a specialization for the 'int' class. There is no way
> that the 'int' class can become a friend of the template class.

This is completely irrelevant. The result could be silently ignored,
or cause an error. In no way is this a reason to disallow such a
useful construct.


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Andrei Alexandrescu" <andrewalex@hotmail.com>
Date: 1999/11/04
Raw View
Hyman Rosen <hymie@prolifics.com> wrote in message
news:t74sf3upnm.fsf@calumny.jyacc.com...
> This is completely irrelevant. The result could be silently ignored,
> or cause an error. In no way is this a reason to disallow such a
> useful construct.

Right. See the 'const const' precedent.


Andrei




[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "TiTi" <TJunkMeBigTime@mad.dog.com>
Date: 1999/11/05
Raw View
I came to the same conclusion myself. But it has a sense of truth. I don't
know how friends are handled, but I presume it is telling the
linker/compiler that a certain class/fn. may access private/protected in
another class. Such info. would have to be stored, and for every template
specialization, a new friend would be born. Since C++ compiler/linker is not
a maternity (don't really know the word for that), it refuses to participate
is such firend spawning.

Elaborate, why is it not possible ...


TiTi



> "TiTi" <TJunkMeBigTime@mad.dog.com> writes:
> > Just think of such a specialization for the 'int' class. There is no way
> > that the 'int' class can become a friend of the template class.
>
> This is completely irrelevant. The result could be silently ignored,
> or cause an error. In no way is this a reason to disallow such a
> useful construct.



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "TiTi" <TJunkMeBigTime@mad.dog.com>
Date: 1999/11/05
Raw View
Elaborate.


TiTi


> Right. See the 'const const' precedent.



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Ron" <nospam@this-address.org>
Date: 1999/11/05
Raw View
TiTi <TJunkMeBigTime@mad.dog.com> wrote in message
news:7vu4mm$otf$1@trex.antw.online.be...
>
> I came to the same conclusion myself. But it has a sense of truth. I don't
> know how friends are handled, but I presume it is telling the
> linker/compiler that a certain class/fn. may access private/protected in
> another class. Such info. would have to be stored, and for every template
> specialization, a new friend would be born. Since C++ compiler/linker is
not
> a maternity (don't really know the word for that), it refuses to
participate
> is such firend spawning.

Ah! But when you specialize a template, the compiler spawns the code for the
resulting class or function. It seems like very little extra effort to spawn
the friend information as well. The more I think about this restriction, the
more curious it seems.

-- Ron



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "TiTi" <TJunkMeBigTime@mad.dog.com>
Date: 1999/11/03
Raw View
Indeed, the standard forbids this use of friendship, and indeed the 7.1.5.3
clause states:

"If the identifier resolves to a class   name or enum   name, the
elaborated   type   specifier introduces it into the decla   ration the same way a
simple   type   specifier introduces its type   name. 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. ] "


Just think of such a specialization for the 'int' class. There is no way
that the 'int' class can become a friend of the template class.



TiTi




> Here's a contrived offending template class for illustration:
>
> template< class OBJECT >
> class Foo
> {
> private:
> friend class OBJECT;
> static OBJECT* NewObject();
> };
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Aaron Brashears" <aaron.brashears@SPAMBOTgravity.com>
Date: 1999/10/30
Raw View
I've put together a small helper class which I declare the parameterized
class as a friend, however, my compiler (msvc++6) refuses to compile it. I
found an article in MS knowledge base which claims: No Access for Friend
Class in a Template Class This behavior is by design as per C++ standard
section 7.1.5.3 paragraph 2. I don't have the C++ standard but is this true?
Why was this language design choice made? It would be really useful to make
sure that only the parameterized class can call functions from the template
class

Here's a contrived offending template class for illustration:

template< class OBJECT >
class Foo
{
private:
friend class OBJECT;
static OBJECT* NewObject();
};

class Bar
{
    static Bar* BuildBar() { return Foo<Bar>::NewObject(); }// compiler
error on this line.
}

Thanks for any insight. :)
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Andrei Alexandrescu" <andrewalex@hotmail.com>
Date: 1999/10/31
Raw View
Aaron Brashears <aaron.brashears@SPAMBOTgravity.com> wrote in message
news:s1k8g3okcsf40@corp.supernews.com...
> I've put together a small helper class which I declare the
parameterized
> class as a friend...

This is indeed illegal. IMHO it should be legal, and I needed it badly
once.


Andrei
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]