Declaring overloads (and template functions).

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Declaring overloads (and template functions).

Rene Rivera-2
First, yes, I'm still working on a development plan which is
unexpectedly rather difficult because of the lack of
implementation/desing docs for the existing code :-( Anyway I'm going to
start posting questions as I both run into them, and as I remember...

One item I've run into many times now, I've answered the same question
in IRC a few times, is how overloads are declared. Most programmers seem
unfamiliar with the function pointer cast method of obtaining a specific
function, either a specific overload or function template instance.
Taking the Luabind example (since Luabind is closer to what we want
Langbinding to model):

===C++
struct A
{
     void f(int);
     void f(int, int);
};

class_<A>()
     .def("f", (void(A::*)(int))&A::f)
===

My idea is that since we have a precedent for decorating the functions,
in the form of "constructor<T>()", we can use a similar decoration to
make the syntax obvious, and to remove some of the repetition. Something
like:

===C++
class_<A>()
     .def("f", overload<void(int)>(&A::f))
===

Though, I'm not sure if one can make such an "overload<>()" function
smart enough to automatically turn the bare function type into the
corresponding member function pointer type.


--
-- Grafik - Don't Assume Anything
-- Redshift Software, Inc. - http://redshift-software.com
-- rrivera/acm.org - grafik/redshift-software.com
-- 102708583/icq - grafikrobot/aim - grafikrobot/yahoo

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Boost-langbinding mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/boost-langbinding
Reply | Threaded
Open this post in threaded view
|

Re: Declaring overloads (and template functions).

Roman Yakovenko
On 7/26/07, Rene Rivera <[hidden email]> wrote:

> One item I've run into many times now, I've answered the same question
> in IRC a few times, is how overloads are declared. Most programmers seem
> unfamiliar with the function pointer cast method of obtaining a specific
> function, either a specific overload or function template instance.
>
> Taking the Luabind example (since Luabind is closer to what we want
> Langbinding to model):
>
> ===C++
> struct A
> {
>      void f(int);
>      void f(int, int);
> };
>
> class_<A>()
>      .def("f", (void(A::*)(int))&A::f)
> ===
>
> My idea is that since we have a precedent for decorating the functions,
> in the form of "constructor<T>()", we can use a similar decoration to
> make the syntax obvious, and to remove some of the repetition. Something
> like:
>
> ===C++
> class_<A>()
>      .def("f", overload<void(int)>(&A::f))
> ===
>
> Though, I'm not sure if one can make such an "overload<>()" function
> smart enough to automatically turn the bare function type into the
> corresponding member function pointer type.

I could be wrong but you can check this with boost::function library.
http://boost.org/doc/html/function/tutorial.html#id1186900 .
If that library can do this, than you can do it too.


--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Boost-langbinding mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/boost-langbinding
Reply | Threaded
Open this post in threaded view
|

Re: Declaring overloads (and template functions).

Rene Rivera-2
Roman Yakovenko wrote:

> On 7/26/07, Rene Rivera <[hidden email]> wrote:
>> ===C++
>> class_<A>()
>>      .def("f", overload<void(int)>(&A::f))
>> ===
>>
>> Though, I'm not sure if one can make such an "overload<>()" function
>> smart enough to automatically turn the bare function type into the
>> corresponding member function pointer type.
>
> I could be wrong but you can check this with boost::function library.
> http://boost.org/doc/html/function/tutorial.html#id1186900 .
> If that library can do this, than you can do it too.

That is equivalent to using is_member_function_pointer
<http://boost.org/doc/html/boost_typetraits/reference.html#boost_typetraits.is_member_function_pointer>.
The problems with those is that we don't have a member function pointer
yet. To be clear the function might be declared as:

template <typename FunctionType, typename FunctionPointerType>
FunctionPointerType overload( FunctionPointerType p )
{
     return p;
}

The hard part is in calculating the FunctionPointerType such that
FunctionType doesn't have include the "A::*" member pointer
qualification. I personally dont't see a way of doing it :-(


--
-- Grafik - Don't Assume Anything
-- Redshift Software, Inc. - http://redshift-software.com
-- rrivera/acm.org - grafik/redshift-software.com
-- 102708583/icq - grafikrobot/aim - grafikrobot/yahoo

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Boost-langbinding mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/boost-langbinding
Reply | Threaded
Open this post in threaded view
|

Re: Declaring overloads (and template functions).

Daniel Wallin-2
Rene Rivera wrote:

> Roman Yakovenko wrote:
>> On 7/26/07, Rene Rivera <[hidden email]> wrote:
>>> ===C++
>>> class_<A>()
>>>      .def("f", overload<void(int)>(&A::f))
>>> ===
>>>
>>> Though, I'm not sure if one can make such an "overload<>()" function
>>> smart enough to automatically turn the bare function type into the
>>> corresponding member function pointer type.
>> I could be wrong but you can check this with boost::function library.
>> http://boost.org/doc/html/function/tutorial.html#id1186900 .
>> If that library can do this, than you can do it too.
>
> That is equivalent to using is_member_function_pointer
> <http://boost.org/doc/html/boost_typetraits/reference.html#boost_typetraits.is_member_function_pointer>.
> The problems with those is that we don't have a member function pointer
> yet. To be clear the function might be declared as:
>
> template <typename FunctionType, typename FunctionPointerType>
> FunctionPointerType overload( FunctionPointerType p )
> {
>      return p;
> }
>
> The hard part is in calculating the FunctionPointerType such that
> FunctionType doesn't have include the "A::*" member pointer
> qualification. I personally dont't see a way of doing it :-(

Here's a prototype:

  template <class T>
  struct types
  {};

  template <class R, class T>
  struct types<R(T)>
  {
      typedef R result;
      typedef T a0;
      typedef int arity1;
  };

  template <class R, class T, class U>
  struct types<R(T,U)>
  {
      typedef R result;
      typedef T a0;
      typedef U a1;
      typedef int arity2;
  };

  template <class T, class C>
  void overload(typename types<T>::result (C::*)(typename types<T>::a0),
    typename types<T>::arity1 = 0);

  template <class T, class C>
  void overload(typename types<T>::result (C::*)(typename types<T>::a0,
    typename types<T>::a1), typename types<T>::arity2 = 0);

  struct A
  {
      void f(int);
      char f(float,int);
  };

  int main()
  {
      overload<char(float,int)>(&A::f);
  }

HTH,
--
Daniel Wallin
Boost Consulting
www.boost-consulting.com


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Boost-langbinding mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/boost-langbinding