Topic: function pointer


Author: orjanlundberg@my-dejanews.com
Date: 1999/04/05
Raw View
You can use ptr_fun defined in "<functional>" to transform a pointer to a
function.

In article <36FA5C48.B0213A27@pegasi.com>,
>     I know there is a declaration in Borland compilers __closure to
> allow you to use function pointer within a class. I'd like to know if
> that is standard or what would be the standard way to do it ?

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ 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/04/05
Raw View
orjanlundberg@my-dejanews.com wrote in message
<7e8hcm$5ht$1@nnrp1.dejanews.com>...
>
>You can use ptr_fun defined in "<functional>" to transform a pointer
to a
>function.
>
>In article <36FA5C48.B0213A27@pegasi.com>,
>>     I know there is a declaration in Borland compilers __closure to
>> allow you to use function pointer within a class. I'd like to know
if
>> that is standard or what would be the standard way to do it ?


ptr_fun is something else. In spite of appearances, mem_fun won't cut
the deal either.

I guess the conspicouos silence about the __closure posting comes from
fatigue. The subject has generated a thread so long, only its printing
would kill a tree. Please search on "closures" on dejanews.

I can't help but add the following:

operator->* is the strangest operator in C++. It is binary, it's
overloadable, however applying the built-in version of it does not
render a value of a type just like the other operators. Its result is
a strange temporary of unknown type, to which you can only apply the
function-call operator, after which it dissapears in the chaos from
which it appeared for a fragile existence...

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: "Andrei Alexandrescu" <andrewalex@hotmail.com>
Date: 1999/04/08
Raw View
Biju Thomas <b_thomas@ibm.net> wrote in message
news:37096560.9019F359@ibm.net...
> Andrei Alexandrescu wrote:
> >
> > operator->* is the strangest operator in C++. It is binary, it's
> > overloadable,
> >
>
> This has been a surprise to me always. I haven't seen any code that
> overloads 'operator->*' anytime. Did any of the people here find any use
> for overloaded operator->*?

It's also been a surprise to me. The problem is that you cannot overload it
properly. Otherwise, any smart pointer ought to overload it. Consider this:

struct T
{
    void f();
};
auto_ptr<T> sp = new T;
T * p = new T;
p->f();    // works
sp->f();    // fine, works due to operator->()
void (T::*pf)() = T::f;
(p->f)();    // works
(sp->*f)();    // error!

I think not having a first-class value for the result of an operator is
unique (and bothering) to operator->*.

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: John_Maddock@compuserve.com (John Maddock)
Date: 1999/04/06
Raw View
>    I know there is a declaration in Borland compilers __closure to
>allow you to use function pointer within a class. I'd like to know if
>that is standard or what would be the standard way to do it ?

Up to a point: if the function takes one parameter then you can get a
function object that is equivalent to a Borland style closure with:

std::bind1st(std::mem_fun(&ClassName::ProcName), ObjectPointer);

Look up binders and the mem_fun types in your standard reference.
Note that this approach is not foolproof.

John Maddock
http://ourworld.compuserve.com/homepages/John_Maddock/
---
[ 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: Biju Thomas <b_thomas@ibm.net>
Date: 1999/04/06
Raw View
Andrei Alexandrescu wrote:
>
> operator->* is the strangest operator in C++. It is binary, it's
> overloadable,
>

This has been a surprise to me always. I haven't seen any code that
overloads 'operator->*' anytime. Did any of the people here find any use
for overloaded operator->*?

Best regards,
Biju Thomas
---
[ 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: Nguyen Hoan Hoang <hoan@pegasi.com>
Date: 1999/03/31
Raw View
>
>
> >     I know there is a declaration in Borland compilers __closure to
> > allow you to use function pointer within a class. I'd like to know if
> > that is standard or what would be the standard way to do it ?
>
> Well, if you don't tell what __closure does (or what you
> want to do), we can't tell you how to do it in standard C++.

Here is the definition of __closure taken out of the online help.

---------------
__closure

The __closure keyword is used to declare a special type of pointer to a
member function. Unlike a regular C++ member function pointer, a closure
contains an object pointer.
In standard C++, you can assign a derived class instance to a base class
pointer; however, you cannot assign a derived class   s member function to a
base class member function pointer. The following code illustrates this:

class base

{
 public:
 void func(int x);
};

class derived: public base

{
 public:
 void new_func(int i);
};

void (base::*bptr)(int);

bptr = &derived::new_func; // illegal

However, the __closure language extension allows you to do this in
C++Builder. A closure associates a pointer to a member function with a
pointer to a class instance. The pointer to the class instance is used as
the this pointer when calling the associated member function. A closure
declaration is the same as a function pointer declaration but with the
addition of the __closure keyword before the identifier being defined. For
example:

struct MyObject

{
  double MemFunc(int);
};

double func1(MyObject *obj)

{
  // A closure taking an int argument and returning double.
  double ( __closure *myClosure )(int);

// Initialize the closure.

  myClosure = obj -> MemFunc;

// Use the closure to call the member function and pass it an int.

  return myClosure(1);
}





[ 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: Nguyen Hoan Hoang <hoan@pegasi.com>
Date: 1999/03/25
Raw View
Hi,

    I know there is a declaration in Borland compilers __closure to
allow you to use function pointer within a class. I'd like to know if
that is standard or what would be the standard way to do it ?
---
[ 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: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/03/26
Raw View
Nguyen Hoan Hoang wrote:

>     I know there is a declaration in Borland compilers __closure to
> allow you to use function pointer within a class. I'd like to know if
> that is standard or what would be the standard way to do it ?

Well, if you don't tell what __closure does (or what you
want to do), we can't tell you how to do it in standard C++.

(BWT, the __ is a strong hint that it's non standard.
(Yes, I know about __LINE__ and co.))

Perhaps what you want is a functor (in STL vocabulary):

struct T {
     void operator()(int i)
     { cout << i * j; }
     int j;
     T (int J) : j (J) {}
};

Here T is a closure: it's a function T::operator()(int)
plus a state. An instance of T is used as it was a
function:

T x (4);

x (5); // prints 20

But x can't be given to a function taking a void (*) (int)
argument.

--

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