Guest User

Untitled

a guest
Jul 19th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3. #include <string>
  4. using namespace std;
  5. class Msg {
  6. public:
  7. void parse(const std::string &s) {
  8. x = int(s[0]);
  9. }
  10. int x = 0;
  11. };
  12. class C {
  13. public:
  14. C(function<void(const std::string &)> cb)
  15. : cb_(cb)
  16. {}
  17. template<typename TMsg>
  18. C(function<void(const TMsg &)> cb)
  19. : C([&, cb](const std::string &s) {
  20. TMsg m;
  21. m.parse(s);
  22. cb(m);
  23. })
  24. {}
  25. void test() {
  26. cb_("foo");
  27. }
  28. function<void(const std::string &)> cb_;
  29. };
  30. void cb1(const string &s) {
  31. cout << "cb1: " << s << endl;
  32. }
  33. void cb2(const Msg &msg) {
  34. cout << "cb2: Msg{x=" << msg.x << "}" << endl;
  35. }
  36. int main() {
  37. C c1(&cb1), c2(&cb2);
  38. c1.test();
  39. c2.test();
  40. return 0;
  41. }
  42.  
  43. test_func_arg_overload.cpp:39:17: error: no matching constructor for initialization of 'C'
  44. C c1(&cb1), c2(&cb2);
  45. ^ ~~~~
  46. test_func_arg_overload.cpp:12:7: note: candidate constructor (the implicit move constructor) not
  47. viable: no known conversion from 'void (*)(const Msg &)' to 'C' for 1st argument
  48. class C {
  49. ^
  50. test_func_arg_overload.cpp:12:7: note: candidate constructor (the implicit copy constructor) not
  51. viable: no known conversion from 'void (*)(const Msg &)' to 'const C' for 1st argument
  52. class C {
  53. ^
  54. test_func_arg_overload.cpp:14:5: note: candidate constructor not viable: no known conversion from
  55. 'void (*)(const Msg &)' to 'function<void (const std::string &)>' (aka 'function<void (const
  56. basic_string<char, char_traits<char>, allocator<char> > &)>') for 1st argument
  57. C(function<void(const std::string &)> cb)
  58. ^
  59. test_func_arg_overload.cpp:19:5: note: candidate template ignored: could not match 'function<void
  60. (const type-parameter-0-0 &)>' against 'void (*)(const Msg &)'
  61. C(function<void(const TMsg &)> cb)
  62. ^
  63. 1 error generated.
Add Comment
Please, Sign In to add comment