Problem: passing function as argument !!!

This is a discussion on Problem: passing function as argument !!! within the C++ Programming forums, part of the General Programming Boards category; >> That's why there are two overloads! One for const, one for non-const. What, you expect me to actually read ...

  1. #16
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> That's why there are two overloads! One for const, one for non-const.

    What, you expect me to actually read the code?

    >> I'm just concerned about returning void from the FncPtr call.

    Code:
    template < typename R >
    R foo( R ( *f )( ) )
    {
    	return f( );
    }
    
    int bar( void )
    {
    	return 3114;
    }
    
    void qux( void )
    {	}
    
    int main( void )
    {
    	cout << foo( bar ) << endl;
    	foo( qux );
    	return 0;
    }
    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;
    }

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Quote Originally Posted by Sebastiani View Post
    >> That's why there are two overloads! One for const, one for non-const.
    What, you expect me to actually read the code?
    Aww, come on. It's not that big and complicated.
    So this actually compiles, where you try to return "void"?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Aww, come on. It's not that big and complicated.

    Oh I know, I should have paid closer attention.

    >> So this actually compiles, where you try to return "void"?

    Yep. As long as you don't try to explicitly declare a variable of that type, though (which is really too bad because that would have been a useful feature). So in other words, this would not compile if used with void:

    Code:
    template < typename R >
    R foo( R ( *f )( ) )
    {
    	R r = f( );		
    	return r;
    }
    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;
    }

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Alright, cool, no worries about that then.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Jul 2009
    Posts
    15
    Quote Originally Posted by Elysia View Post
    Two examples.
    One using boost, the other without boost.
    Code:
    class ExClass 
    {
    public:
        int func(const int& z); //***
    };
    
    int ExClass::func(const int& z) { return z; }
    
    template <typename T>
    void example(T& func) { func(); }
    
    template <typename Class_t, typename Fnc_t>
    void example2(Class_t& Class, Fnc_t FncPtr) { (Class.*FncPtr)(); }
    
    int main()
    {
         ExClass f;
         example( boost::bind(&ExClass::func, f) );
         example2(f, &ExClass::func);
         return 0;
    }
    And don't forget to read this:
    https://apps.sourceforge.net/mediawi...arameter_names
    I cant read this because its looked...

    And, I cant see but when I am compiling that, its returning a error:
    Code:
     In function ‘void example2(Class_t&, Fnc_t) [with Class_t = ExClass, Fnc_t = int (ExClass::*)(const int&)]’:
    16:   instantiated from here
    10: error: too few arguments to function
    What is wrong?

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Ah, that's because the function takes an int, but we don't pass one. You can fix it like:
    (Class.*FncPtr)(10);
    func(10);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    Jul 2009
    Posts
    15
    Quote Originally Posted by Elysia View Post
    Ah, that's because the function takes an int, but we don't pass one. You can fix it like:
    (Class.*FncPtr)(10);
    func(10);
    hmmmm that is what I was trying to avoid...
    But I'm trying to use boost::function...

    Do you know some really good book to indicate to me??? with this kind of doubt

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Oh? Then let's borrow the power of boost::bind a little more:
    example( boost::bind(&ExClass::func, f, 10) );
    Notice that I put a number in there as well.
    Now
    func();
    Simply will work.
    Praised be boost!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Jul 2009
    Posts
    15
    What probably will solve my problem is:
    Code:
    ExClass f;
    f.func = boost::bind( &ExClass::func, &f );
    example( f.func );
    isn't?

    But, do I really need to install all boost library to use boost::bind()???

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    It doesn't work that way! Just pass what boost::bind returns directly to your function.
    You need to download the boost package, yes, but you don't need to install any dlls to use it. However, you can't just break it into pieces since it has dependencies on other headers in the boost distribution.

    Code:
    ExClass f;
    example( boost::bind(&ExClass::func, f, 1000) );
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User
    Join Date
    Jul 2009
    Posts
    15
    Quote Originally Posted by Elysia View Post
    It doesn't work that way! Just pass what boost::bind returns directly to your function.
    You need to download the boost package, yes, but you don't need to install any dlls to use it. However, you can't just break it into pieces since it has dependencies on other headers in the boost distribution.

    Code:
    ExClass f;
    example( boost::bind(&ExClass::func, f, 1000) );
    Ok, and how about the book???
    Can you suggest one??
    To look up that kind of doubt?

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Not much about boost, sadly.
    There is one book about boost, I think. Can't remember its name. It's somewhat old and doesn't cover that much, but it's about the only one available or so. A search on the web should reveal more information about that.
    Otherwise the boost homepage is your best source of information.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Jul 2009
    Posts
    15
    Quote Originally Posted by Elysia View Post
    Not much about boost, sadly.
    There is one book about boost, I think. Can't remember its name. It's somewhat old and doesn't cover that much, but it's about the only one available or so. A search on the web should reveal more information about that.
    Otherwise the boost homepage is your best source of information.
    No no, not about boost... about c++, but not too beginer, I say intermediate!!!!

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Oh, okay. Then check the books thread.
    If you haven't checked out Accelerated C++, then you should probably do that too.
    It doesn't cover just the basics; it covers a lot of more and a lot of it is handy stuff!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Registered User
    Join Date
    Jul 2009
    Posts
    15
    Quote Originally Posted by Elysia View Post
    Oh, okay. Then check the books thread.
    If you haven't checked out Accelerated C++, then you should probably do that too.
    It doesn't cover just the basics; it covers a lot of more and a lot of it is handy stuff!
    Ok, thank you all your attention and patient to my problem!!!!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing A Function Into A Constructor
    By fourplay in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2009, 07:06 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Passing function as argument
    By NeMewSys in forum C Programming
    Replies: 16
    Last Post: 06-11-2008, 03:03 PM
  4. passing a function as an argument
    By angelscars in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2005, 09:53 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM

Tags for this Thread


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