Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2002-02-14 04:29:20


Mark Rodgers wrote:
>
> From: "Daniel Frey" <daniel.frey_at_[hidden]>
>
> > template< typename T > class finalize
> > {
> > friend T;
> > finalize() {}
> > };
>
> This has been suggested many times in the past but unfortunately
> the standard gets in the way. See 7.1.5.3p2:
>
> "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. ]"

I do not have the standard, but I always thought that 'friend T;' is
legal while 'friend class T;' isn't. Are you saying that they are both
illegal? Can someone please confirm this? If yes: Is there any (legal)
way to make a template parameter a friend?

> Otherwise, yes, your finalize template would be a useful addition
> to utility.hpp, although I'd prefer a different name maybe "key"
> since it had other uses before it was made illegal. For example,
> you could use it to restrict a function so that only a certain class
> can call it:
>
> [example snipped]
>
> Since only Y can construct a key<Y>, no-one else can call X::f
> (unless Y gave them the key).

I'd like to see a reasonable example where this is needed. If I have a
function that should only be called by Y, it'd be a private function of
Y or X would be a friend of Y.

Anyway, you need to think of the resulting error and what it tells the
user. 'finalize' is a good name to give the user a hint of what's wrong.
Probably a second, mostly identical class with a better name is needed
for your case as I (personal opinion follows...) can not see why it
should be called 'key'. Probably 'restrict'? But I don't think there is
a general name for all cases.

Regards, Daniel

--
Daniel Frey
aixigo AG - financial training, research and technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey_at_[hidden], web: http://www.aixigo.de

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk