From 2779150034611406994
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,9d57bb38940f08c2
X-Google-Attributes: gidf78e5,public
From: sbnaran@localhost.localdomain.COM (Siemel Naran)
Subject: Re: References to functions allowed?
Date: 1999/01/18
Message-ID: <slrn7a5me3.7lr.sbnaran@localhost.localdomain>#1/1
X-Deja-AN: 434032764
X-NNTP-Posting-Host: rochester-69.slip.uiuc.edu
Approved: stephen.clamage@sun.com (comp.std.c++)
References: <36A222B3.E51DFD28@ibm.net>
X-UID: 0000000001
X-Status: $$$T
Organization: University of Illinois at Urbana-Champaign
Reply-To: sbnaran@uiuc.edu
Newsgroups: comp.std.c++
Originator: clamage@taumet


On 18 Jan 99 03:17:34 GMT, Biju Thomas <bijuthom@ibm.net> wrote:

>Does the C++ standard allow references to functions? For example, is the
>following legal?
>
>int f () {}
>
>int (&rf)() = f;

In discussing functions like
  template <class Iter, class Predicate>
  Predicate for_each(Iter begin, Iter end, Predicate pred);
the standard says that the function object 'pred' must be a pointer
to function, or an object with a function call operator (see, eg,
chapter 25, item 7).

Nothing is said of a reference to a function.  This is because
references to functions add nothing new to the language.  After all,
when dereferencing a pointer to a function, we don't need to use
the '*' operator.  This freedom is necessary because the the
Predicate object in functions like for_each might be
   [X] function objects, for which the '*' operator should not be used
   [X] pointers to function, for which the '*' may or may not be used
This allows for_each to be written like this:
   template <class Iter, class Predicate>
    Predicate for_each(Iter begin, const Iter end, Predicate pred) {
       while (begin!=end) pred(*begin);
       return pred;
    }

But references to functions are legal.  The compiler 'como' accepts
it.  From chapter 13.4 item 3, we read

   Non-member functions and static member functions match targets
   of type "pointer-to-function" or "reference-to-function".
   Nonstatic member functions match targets of type
   "pointer-to-member-function"; ...

There is no such thing as a reference to a member function.  The
term is mentioned in other places, like chapter 8.3.5 item 4, and
section 13.3 item 2.  Whether references to a functions are a
useful concept, I don't know, but I think the answer is no.

-- 
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------


[ 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              ]




