Obtaining function signature

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • psujkov@gmail.com

    #1

    Obtaining function signature

    Hi everybody,

    int f(int a, int b) { return a + b; };
    is it possible to obtain this function signature - int (int, int) in
    this case - for use in boost::function _traits ?
    e.g. std::cout << "f's arity : " << boost::function _traits<*obtain ing
    signature from f()*>::arity << std::endl;
    please no macro solutions - only C++ (Boost MPL maybe could be useful
    - but still don't see how)

    best regards, Paul Sujkov

  • Jim Langston

    #2
    Re: Obtaining function signature

    <psujkov@gmail. comwrote in message
    news:1171031521 .588934.119190@ h3g2000cwc.goog legroups.com...
    Hi everybody,
    >
    int f(int a, int b) { return a + b; };
    is it possible to obtain this function signature - int (int, int) in
    this case - for use in boost::function _traits ?
    e.g. std::cout << "f's arity : " << boost::function _traits<*obtain ing
    signature from f()*>::arity << std::endl;
    please no macro solutions - only C++ (Boost MPL maybe could be useful
    - but still don't see how)
    >
    best regards, Paul Sujkov
    The only thing I can come up with is that the output of the following
    program is:
    int __cdecl(int,int )

    but this may be OS specific. I do not know if the .name() of typeid is
    defined in the standard. If your program doesn't have to be compiled on
    more than one platform, then you may consider doing something like this.

    #include <iostream>

    int f(int a, int b) { return a + b; };

    int main ()
    {
    std::cout << typeid( f ).name();

    std::cin.get();
    }


    Comment

    • Kai-Uwe Bux

      #3
      Re: Obtaining function signature

      psujkov@gmail.c om wrote:
      Hi everybody,
      >
      int f(int a, int b) { return a + b; };
      is it possible to obtain this function signature - int (int, int) in
      this case - for use in boost::function _traits ?
      e.g. std::cout << "f's arity : " << boost::function _traits<*obtain ing
      signature from f()*>::arity << std::endl;
      please no macro solutions - only C++ (Boost MPL maybe could be useful
      - but still don't see how)
      I don't see any way of doing that. On the other hand, since C++ is strongly
      typed, in any context where you have the function, you know its type. So, I
      wonder which problem you are trying to solve.


      Best

      Kai-Uwe Bux

      Comment

      • Mark P

        #4
        Re: Obtaining function signature

        Jim Langston wrote:
        <psujkov@gmail. comwrote in message
        news:1171031521 .588934.119190@ h3g2000cwc.goog legroups.com...
        >Hi everybody,
        >>
        >int f(int a, int b) { return a + b; };
        >is it possible to obtain this function signature - int (int, int) in
        >this case - for use in boost::function _traits ?
        >e.g. std::cout << "f's arity : " << boost::function _traits<*obtain ing
        >signature from f()*>::arity << std::endl;
        >please no macro solutions - only C++ (Boost MPL maybe could be useful
        >- but still don't see how)
        >>
        >best regards, Paul Sujkov
        >
        The only thing I can come up with is that the output of the following
        program is:
        int __cdecl(int,int )
        >
        but this may be OS specific. I do not know if the .name() of typeid is
        defined in the standard. If your program doesn't have to be compiled on
        more than one platform, then you may consider doing something like this.
        >
        #include <iostream>
        #include <typeinfo>
        >
        int f(int a, int b) { return a + b; };
        >
        int main ()
        {
        std::cout << typeid( f ).name();
        >
        std::cin.get();
        }
        >
        >
        name() is a standard function (of type_info) however the output of this
        function, besides that it's a null-terminated string, is unspecified.

        Comment

        • Dave Rahardja

          #5
          Re: Obtaining function signature

          On Fri, 09 Feb 2007 23:00:38 GMT, Mark P
          <usenet@fall200 5REMOVE.fastmai lCAPS.fmwrote:
          >Jim Langston wrote:
          ><psujkov@gmail .comwrote in message
          >news:117103152 1.588934.119190 @h3g2000cwc.goo glegroups.com.. .
          >>Hi everybody,
          >>>
          >>int f(int a, int b) { return a + b; };
          >>is it possible to obtain this function signature - int (int, int) in
          >>this case - for use in boost::function _traits ?
          >>e.g. std::cout << "f's arity : " << boost::function _traits<*obtain ing
          >>signature from f()*>::arity << std::endl;
          >>please no macro solutions - only C++ (Boost MPL maybe could be useful
          >>- but still don't see how)
          >>>
          >>best regards, Paul Sujkov
          >>
          >The only thing I can come up with is that the output of the following
          >program is:
          >int __cdecl(int,int )
          >>
          >but this may be OS specific. I do not know if the .name() of typeid is
          >defined in the standard. If your program doesn't have to be compiled on
          >more than one platform, then you may consider doing something like this.
          >>
          >#include <iostream>
          >
          >#include <typeinfo>
          >
          >>
          >int f(int a, int b) { return a + b; };
          >>
          >int main ()
          >{
          > std::cout << typeid( f ).name();
          >>
          > std::cin.get();
          >}
          >>
          >>
          >
          >name() is a standard function (of type_info) however the output of this
          >function, besides that it's a null-terminated string, is unspecified.
          But you can't use the result of typeid as a template parameter for
          boost::function _traits.

          -dr

          Comment

          • paul.joseph.davis@gmail.com

            #6
            Re: Obtaining function signature

            On Feb 9, 10:35 pm, Dave Rahardja <a...@me.comwro te:
            On Fri, 09 Feb 2007 23:00:38 GMT, Mark P
            >
            >
            >
            <use...@fall200 5REMOVE.fastmai lCAPS.fmwrote:
            Jim Langston wrote:
            <psuj...@gmail. comwrote in message
            >news:117103152 1.588934.119190 @h3g2000cwc.goo glegroups.com.. .
            >Hi everybody,
            >
            >int f(int a, int b) { return a + b; };
            >is it possible to obtain this function signature - int (int, int) in
            >this case - for use in boost::function _traits ?
            >e.g. std::cout << "f's arity : " << boost::function _traits<*obtain ing
            >signature from f()*>::arity << std::endl;
            >please no macro solutions - only C++ (Boost MPL maybe could be useful
            >- but still don't see how)
            >
            >best regards, Paul Sujkov
            >
            The only thing I can come up with is that the output of the following
            program is:
            int __cdecl(int,int )
            >
            but this may be OS specific. I do not know if the .name() of typeid is
            defined in the standard. If your program doesn't have to be compiled on
            more than one platform, then you may consider doing something like this.
            >
            #include <iostream>
            >
            #include <typeinfo>
            >
            int f(int a, int b) { return a + b; };
            >
            int main ()
            {
            std::cout << typeid( f ).name();
            >
            std::cin.get();
            }
            >
            name() is a standard function (of type_info) however the output of this
            function, besides that it's a null-terminated string, is unspecified.
            >
            But you can't use the result of typeid as a template parameter for
            boost::function _traits.
            >
            -dr

            But you sure can use typeof() to do this. There's problems with
            boost::function _traits when using a member function though. So I've
            taken entirely too long to try and figure something clever out, but
            this is as good as I got:

            #include <iostream>

            #include <boost/type_traits/function_traits .hpp>

            template< typename R struct fn_def0
            { static R fn() {} } ;

            template< typename R, typename T int mem_fn_arity( R ( T::* )
            (void) )
            {
            return boost::function _traits< typeof( fn_def0<R>::fn ) >::arity ;
            }

            template< typename R, typename A1 struct fn_def1
            { static R fn( A1 ) {} } ;

            template< typename R, typename T, typename A1 int mem_fn_arity( R
            ( T::* )( A1 ) )
            {
            return boost::function _traits< typeof( fn_def1<R,A1>:: fn )
            >::arity ;
            }

            template< typename R, typename A1, typename A2 struct fn_def2
            { static R fn( A1, A2 ) {} } ;

            template< typename R, typename T, typename A1, typename A2 int
            mem_fn_arity( R ( T::* )( A1, A2 ) )
            {
            return boost::function _traits< typeof( fn_def2<R,A1,A2 >::fn )
            >::arity ;
            }

            class A
            {
            public:
            int foo0() {}
            int foo1( int a ) {}
            int foo2( int a, int b ) {}
            } ;

            int
            main( int argc, char* argv[] )
            {
            std::cout << boost::function _traits< typeof( main ) >::arity <<
            std::endl ;
            std::cout << mem_fn_arity( &A::foo0 ) << std::endl ;
            std::cout << mem_fn_arity( &A::foo1 ) << std::endl ;
            std::cout << mem_fn_arity( &A::foo2 ) << std::endl ;
            }

            HTH,
            Paul

            Comment

            • paul.joseph.davis@gmail.com

              #7
              Re: Obtaining function signature

              On Feb 9, 11:17 pm, "paul.joseph.da ...@gmail.com"
              <paul.joseph.da ...@gmail.comwr ote:
              On Feb 9, 10:35 pm, Dave Rahardja <a...@me.comwro te:
              >
              >
              >
              On Fri, 09 Feb 2007 23:00:38 GMT, Mark P
              >
              <use...@fall200 5REMOVE.fastmai lCAPS.fmwrote:
              >Jim Langston wrote:
              ><psuj...@gmail .comwrote in message
              >>news:11710315 21.588934.11919 0@h3g2000cwc.go oglegroups.com. ..
              >>Hi everybody,
              >
              >>int f(int a, int b) { return a + b; };
              >>is it possible to obtain this function signature - int (int, int) in
              >>this case - for use in boost::function _traits ?
              >>e.g. std::cout << "f's arity : " << boost::function _traits<*obtain ing
              >>signature from f()*>::arity << std::endl;
              >>please no macro solutions - only C++ (Boost MPL maybe could be useful
              >>- but still don't see how)
              >
              >>best regards, Paul Sujkov
              >
              >The only thing I can come up with is that the output of the following
              >program is:
              >int __cdecl(int,int )
              >
              >but this may be OS specific. I do not know if the .name() of typeid is
              >defined in the standard. If your program doesn't have to be compiled on
              >more than one platform, then you may consider doing something like this.
              >
              >#include <iostream>
              >
              >#include <typeinfo>
              >
              >int f(int a, int b) { return a + b; };
              >
              >int main ()
              >{
              > std::cout << typeid( f ).name();
              >
              > std::cin.get();
              >}
              >
              >name() is a standard function (of type_info) however the output of this
              >function, besides that it's a null-terminated string, is unspecified.
              >
              But you can't use the result of typeid as a template parameter for
              boost::function _traits.
              >
              -dr
              >
              But you sure can use typeof() to do this. There's problems with
              boost::function _traits when using a member function though. So I've
              taken entirely too long to try and figure something clever out, but
              this is as good as I got:
              >
              #include <iostream>
              >
              #include <boost/type_traits/function_traits .hpp>
              >
              template< typename R struct fn_def0
              { static R fn() {} } ;
              >
              template< typename R, typename T int mem_fn_arity( R ( T::* )
              (void) )
              {
              return boost::function _traits< typeof( fn_def0<R>::fn ) >::arity ;
              >
              }
              >
              template< typename R, typename A1 struct fn_def1
              { static R fn( A1 ) {} } ;
              >
              template< typename R, typename T, typename A1 int mem_fn_arity( R
              ( T::* )( A1 ) )
              {
              return boost::function _traits< typeof( fn_def1<R,A1>:: fn )
              >
              ::arity ;
              }
              >
              template< typename R, typename A1, typename A2 struct fn_def2
              { static R fn( A1, A2 ) {} } ;
              >
              template< typename R, typename T, typename A1, typename A2 int
              mem_fn_arity( R ( T::* )( A1, A2 ) )
              {
              return boost::function _traits< typeof( fn_def2<R,A1,A2 >::fn )
              >
              ::arity ;
              }
              >
              class A
              {
              public:
              int foo0() {}
              int foo1( int a ) {}
              int foo2( int a, int b ) {}
              >
              } ;
              >
              int
              main( int argc, char* argv[] )
              {
              std::cout << boost::function _traits< typeof( main ) >::arity <<
              std::endl ;
              std::cout << mem_fn_arity( &A::foo0 ) << std::endl ;
              std::cout << mem_fn_arity( &A::foo1 ) << std::endl ;
              std::cout << mem_fn_arity( &A::foo2 ) << std::endl ;
              >
              }
              >
              HTH,
              Paul

              So I really suck, cause I just realized this isn't in the standard.
              But it works on compilers that support it :D

              See this article:


              Paul Davis.

              Comment

              • Dave Rahardja

                #8
                Re: Obtaining function signature

                On 9 Feb 2007 21:17:03 -0800, "paul.joseph.da vis@gmail.com"
                <paul.joseph.da vis@gmail.comwr ote:
                >But you sure can use typeof() to do this. There's problems with
                >boost::functio n_traits when using a member function though. So I've
                >taken entirely too long to try and figure something clever out, but
                >this is as good as I got:
                Unfortunately typeof() is not part of the C++ standard.

                -dr

                Comment

                • GRINFY

                  #9
                  Re: Obtaining function signature

                  >From Infinity:

                  Code snippet:
                  -----------------------------------------------------------
                  #include <iostream>
                  using namespace std;

                  int foo(int a, char * b) { return 0; };
                  char * goo(int * a, string b) { return ""; };

                  int main ()
                  {
                  cout << typeid( foo ).name() << "\n";
                  cout << typeid( foo ).raw_name() << "\n\n";
                  cout << typeid( goo ).name() << "\n";
                  cout << typeid( goo ).raw_name() << "\n";
                  cin.get();

                  return 0;
                  }

                  Output:
                  -----------------------------------------------------------
                  int (__cdecl*)(int, char *)
                  ..P6AHHPAD@Z

                  char * (__cdecl*)(int *,class std::basic_stri ng<char,struct
                  std::char_trait s<char>,class std::allocator< char)
                  ..P6APADPAHV?$b asic_string@DU? $char_traits@D@ std@@V?
                  $allocator@D@2@ @std@@@Z


                  .... Take care


                  Comment

                  • Victor Bazarov

                    #10
                    Re: Obtaining function signature

                    GRINFY wrote:
                    >From Infinity:
                    >
                    Code snippet:
                    -----------------------------------------------------------
                    #include <iostream>
                    using namespace std;
                    >
                    int foo(int a, char * b) { return 0; };
                    char * goo(int * a, string b) { return ""; };
                    >
                    int main ()
                    {
                    cout << typeid( foo ).name() << "\n";
                    cout << typeid( foo ).raw_name() << "\n\n";
                    cout << typeid( goo ).name() << "\n";
                    cout << typeid( goo ).raw_name() << "\n";
                    cin.get();
                    >
                    return 0;
                    }
                    >
                    Output:
                    -----------------------------------------------------------
                    int (__cdecl*)(int, char *)
                    .P6AHHPAD@Z
                    >
                    char * (__cdecl*)(int *,class std::basic_stri ng<char,struct
                    std::char_trait s<char>,class std::allocator< char)
                    .P6APADPAHV?$ba sic_string@DU?$ char_traits@D@s td@@V?
                    $allocator@D@2@ @std@@@Z
                    >
                    >
                    ... Take care
                    Just a note: the Standard does NOT require for the implementation
                    to produce any meaningful output from 'std::typeinfo: :name()', and
                    the strings returned are definitely implementation-specific.

                    V
                    --
                    Please remove capital 'A's when replying by e-mail
                    I do not respond to top-posted replies, please don't ask


                    Comment

                    Working...