how to use functors as ordinary functions argument?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laikon

    how to use functors as ordinary functions argument?

    dear, all:

    below is a function with a parameter of function pointer.

    void f(int a, int b, int (*fp)(int, int))
    {
    std::cout << fp(a, b) << std::endl;
    }


    and f is called with functor plus as 3rd argument.

    int x = 2, y = 3;
    f(x, y, std::plus<int>( ));

    but it raises an error, and how to solve it?

    Thanks a lot.
  • red floyd

    #2
    Re: how to use functors as ordinary functions argument?

    laikon wrote:
    dear, all:
    >
    below is a function with a parameter of function pointer.
    >
    void f(int a, int b, int (*fp)(int, int))
    {
    std::cout << fp(a, b) << std::endl;
    }
    >
    >
    and f is called with functor plus as 3rd argument.
    >
    int x = 2, y = 3;
    f(x, y, std::plus<int>( ));
    >
    but it raises an error, and how to solve it?
    overload f.

    #include <functional>
    void f(int a, int b, const std::binary_fun ction<int, int, int>& f)
    {
    std::cout << f(a,b) << std::endl;
    }

    Comment

    • Paul Brettschneider

      #3
      Re: how to use functors as ordinary functions argument?

      red floyd wrote:
      laikon wrote:
      >dear, all:
      >>
      >below is a function with a parameter of function pointer.
      >>
      >void f(int a, int b, int (*fp)(int, int))
      >{
      >std::cout << fp(a, b) << std::endl;
      >}
      >>
      >>
      >and f is called with functor plus as 3rd argument.
      >>
      >int x = 2, y = 3;
      >f(x, y, std::plus<int>( ));
      >>
      >but it raises an error, and how to solve it?
      >
      overload f.
      >
      #include <functional>
      void f(int a, int b, const std::binary_fun ction<int, int, int>& f)
      {
      std::cout << f(a,b) << std::endl;
      }
      Or, since the function body is the same, use templates?

      #include <iostream>

      template <typename Tvoid f(int a, int b, const T &fp)
      {
      std::cout << fp(a, b) << std::endl;
      }

      int mult(int a, int b)
      {
      return a * b;
      }

      int main()
      {
      int x = 2, y = 3;

      f(x, y, std::plus<int>( ));
      f(x, y, mult);
      }

      Comment

      • Gianni Mariani

        #4
        Re: how to use functors as ordinary functions argument?

        laikon wrote:
        dear, all:
        >
        below is a function with a parameter of function pointer.
        >
        void f(int a, int b, int (*fp)(int, int))
        {
        std::cout << fp(a, b) << std::endl;
        }
        >
        >
        and f is called with functor plus as 3rd argument.
        >
        int x = 2, y = 3;
        f(x, y, std::plus<int>( ));
        >
        but it raises an error, and how to solve it?
        Turn f into a template ?

        Comment

        • Juha Nieminen

          #5
          Re: how to use functors as ordinary functions argument?

          laikon wrote:
          dear, all:
          >
          below is a function with a parameter of function pointer.
          >
          void f(int a, int b, int (*fp)(int, int))
          {
          std::cout << fp(a, b) << std::endl;
          }
          >
          >
          and f is called with functor plus as 3rd argument.
          >
          int x = 2, y = 3;
          f(x, y, std::plus<int>( ));
          >
          but it raises an error, and how to solve it?
          Make it a template:

          template<typena me Functor>
          void f(int a, int b, Functor fp)
          {
          std::cout << fp(a, b) << std::endl;
          }

          Comment

          • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

            #6
            Re: how to use functors as ordinary functions argument?

            On 2008-04-01 19:00, laikon wrote:
            dear, all:
            >
            below is a function with a parameter of function pointer.
            >
            void f(int a, int b, int (*fp)(int, int))
            {
            std::cout << fp(a, b) << std::endl;
            }
            >
            >
            and f is called with functor plus as 3rd argument.
            >
            int x = 2, y = 3;
            f(x, y, std::plus<int>( ));
            >
            but it raises an error, and how to solve it?
            Use a function, things that uses functors are built using templates.

            --
            Erik Wikström

            Comment

            • Pete Becker

              #7
              Re: how to use functors as ordinary functions argument?

              On 2008-04-01 13:50:03 -0400, red floyd <no.spam@here.d udesaid:
              >
              overload f.
              >
              #include <functional>
              void f(int a, int b, const std::binary_fun ction<int, int, int>& f)
              {
              std::cout << f(a,b) << std::endl;
              }
              That won't work. std::binary_fun ction has three typedefs, nothing more.

              --
              Pete
              Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
              Standard C++ Library Extensions: a Tutorial and Reference
              (www.petebecker.com/tr1book)

              Comment

              • red floyd

                #8
                Re: how to use functors as ordinary functions argument?

                Pete Becker wrote:
                On 2008-04-01 13:50:03 -0400, red floyd <no.spam@here.d udesaid:
                >
                >>
                >overload f.
                >>
                >#include <functional>
                >void f(int a, int b, const std::binary_fun ction<int, int, int>& f)
                >{
                > std::cout << f(a,b) << std::endl;
                >}
                >
                That won't work. std::binary_fun ction has three typedefs, nothing more.
                >
                Oops... you're right. My bad.

                Comment

                • red floyd

                  #9
                  Re: how to use functors as ordinary functions argument?

                  Juha Nieminen wrote:
                  laikon wrote:
                  >dear, all:
                  >>
                  >below is a function with a parameter of function pointer.
                  >>
                  >void f(int a, int b, int (*fp)(int, int))
                  >{
                  > std::cout << fp(a, b) << std::endl;
                  >}
                  >>
                  >>
                  >and f is called with functor plus as 3rd argument.
                  >>
                  >int x = 2, y = 3;
                  >f(x, y, std::plus<int>( ));
                  >>
                  >but it raises an error, and how to solve it?
                  >
                  Make it a template:
                  >
                  template<typena me Functor>
                  void f(int a, int b, Functor fp)
                  {
                  std::cout << fp(a, b) << std::endl;
                  }
                  Actually, since the OP wanted an int return:

                  template<typena me Functor>
                  void f(int a, int b, Functor fp)
                  {
                  std::cout << static_cast<int >(fp(a,b)) << std::endl;
                  }

                  Comment

                  • Juha Nieminen

                    #10
                    Re: how to use functors as ordinary functions argument?

                    Kai-Uwe Bux wrote:
                    I guess, one could devise some template magic to implement and check the
                    conceptual requirement that Functor has an operator( int, int ) that
                    returns an int. However, I think that would be overkill in most cases.
                    If I'm not mistaken, this will become easy with the next C++ standard.

                    Comment

                    Working...