Is this claim correct: "Anywhere you pass in a function, you can pass in a functor"?

This is a discussion on Is this claim correct: "Anywhere you pass in a function, you can pass in a functor"? within the C++ Programming forums, part of the General Programming Boards category; Is this claim correct: "Anywhere you pass in a function, you can pass in a functor"? for example: Code: bool ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Is this claim correct: "Anywhere you pass in a function, you can pass in a functor"?

    Is this claim correct: "Anywhere you pass in a function, you can pass in a functor"?

    for example:
    Code:
    bool cmp_function(int a, int b){
    return a<b;
    }
    
    struct cmp_functor{
    bool operator()(int a, int b){
    return a<b;
    }
    };
    
    std::sort(vec.begin(), vec.end(), cmp_function);
    std::sort(vec.begin(), vec.end(), cmp_functor);

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    no, that is incorrect. but you can often structure your code in a way that allows either to be used. for example:

    Code:
    template <typename Function>
    void
    foo(Function callback, int data)
    {
    	callback(data);
    }
    
    void
    bar(int)
    {	}
    
    struct baz
    {
    	void
    	operator () (int)
    	{	}
    };
    
    foo(bar, 1);
    foo(baz(), 1);
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thank you, I am curious why "baz()" should carry "()". Isn't "baz" enough?

    Also, in your example, what would the 2 signatures be if the foo only accepts function or funcotr?
    Last edited by meili100; 11-21-2007 at 06:21 PM.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    > I am curious why "baz()" should carry "()". Isn't "baz" enough?
    Because baz is an object that needs to be instantiated, just like other classes or structs in C++.

    > what would the 2 signatures be if the foo only accepts function or funcotr?
    The simplest of the possibilities is probably

    void foo( void (*callback )( int ), int data );
    void foo( baz callback, int data );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21