Skip to content

Instantly share code, notes, and snippets.

@cout
Created July 10, 2014 19:57
Show Gist options
  • Save cout/8b0dbab304a9cf5bed2d to your computer and use it in GitHub Desktop.
Save cout/8b0dbab304a9cf5bed2d to your computer and use it in GitHub Desktop.
generic std::function ?
#include <iostream>
#include <functional>
class Foo { };
class MyMessage { };
template<typename Msg_T>
void foo(
Msg_T const & msg,
std::function<Foo(Msg_T const &)> callback)
{
auto f = callback(msg);
}
int main()
{
MyMessage msg;
foo(msg, [&](MyMessage const &) -> Foo { return Foo(); });
}
/*
pbrannan@twin1a:~/tmp $ clang++ test.cpp -std=c++11
test.cpp:18:3: error: no matching function for call to 'foo'
foo(msg, [&](MyMessage const &) -> Foo { return Foo(); });
^~~
test.cpp:8:6: note: candidate template ignored: could not match 'function<Foo (const type-parameter-0-0 &)>' against '<lambda at test.cpp:18:12>'
void foo(
^
1 error generated.
pbrannan@twin1a:~/tmp $ g++ test.cpp -std=c++11
test.cpp: In function ‘int main()’:
test.cpp:18:59: error: no matching function for call to ‘foo(MyMessage&, main()::__lambda0)’
foo(msg, [&](MyMessage const &) -> Foo { return Foo(); });
^
test.cpp:18:59: note: candidate is:
test.cpp:8:6: note: template<class Msg_T> void foo(const Msg_T&, std::function<Foo(const Msg_T&)>)
void foo(
^
test.cpp:8:6: note: template argument deduction/substitution failed:
test.cpp:18:59: note: ‘main()::__lambda0’ is not derived from ‘std::function<Foo(const Msg_T&)>’
foo(msg, [&](MyMessage const &) -> Foo { return Foo(); });
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment