Lambda as argument to functor

This is a discussion on Lambda as argument to functor within the C++ Programming forums, part of the General Programming Boards category; Foo works as expected. The "Bar" function object does not. For starters, why can I pass a lambda as an ...

  1. #1
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168

    Lambda as argument to functor

    Foo works as expected. The "Bar" function object does not. For starters, why can I pass a lambda as an argument to Foo, but not to Bar?

    Also, how can I achieve the construction of a bar like I'm trying to do in main?

    Code:
    template <class _Fxn>
    void Foo(_Fxn fxn)
    {
        fxn();
    }
    
    template <class _Fxn>
    class Bar
    {
        _Fxn fxn;
    
    public:
    
        Bar(_Fxn f) : fxn(f) {}
        void operator()(){ fxn(); }
    };
    
    int main()
    {
        Foo([]()->void{ std::cout << "Hello World!\n"; });
    
        Bar<void()> bar([]()->void{ std::cout << "Hello World!\n"; }); // DOESN'T WORK
    
    }
    This is the error I get for trying to construct the bar in main:

    Code:
    Error	1	error C2664: 'Bar<_Fxn>::Bar(_Fxn (__cdecl *))' : cannot convert parameter 1 from '`anonymous-namespace'::<lambda1>' to 'void (__cdecl *)(void)'	c:\projects\_cs350\helix\helix.cpp	30	1	Helix
    goto( comeFrom() );

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Like the error says, you are saying that the type _Fxn for Bar is void(), but the actual type for a lambda isn't that. It's something else.
    Perhaps you should try
    Code:
    auto lambda = []() { std::cout << "Hello World!\n"; };
    Bar<decltype(lambda)> bar(lambda);
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error: expected identifier or ( before { token
    By jpcanaverde in forum C Programming
    Replies: 66
    Last Post: 06-08-2010, 12:53 PM
  2. Signature of function accepting functor as argument?
    By StainedBlue in forum C++ Programming
    Replies: 10
    Last Post: 06-01-2010, 04:50 PM
  3. Simple Question - std::vector as argument
    By ejohns85 in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2010, 04:08 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 08:45 PM

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