fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. struct protocol_callbacks
  5. {
  6. using func_t = std::function<int(uint8_t*, size_t)>;
  7.  
  8. protocol_callbacks(func_t a_reader, func_t a_writer) :
  9. reader(a_reader),
  10. writer(a_writer) {}
  11. func_t reader;
  12. func_t writer;
  13. };
  14.  
  15. int writer(uint8_t*, size_t)
  16. {
  17. std::cout << "function writer\n";
  18. return 0;
  19. }
  20. int reader(uint8_t*, size_t)
  21. {
  22. std::cout << "function writer\n";
  23. return 0;
  24. }
  25.  
  26. int bad_writer(uint16_t*, size_t) { return 0; }
  27.  
  28. int main ()
  29. {
  30. int x = 9;
  31. protocol_callbacks pc1(reader, writer);
  32. protocol_callbacks pc2([=](uint8_t*, size_t)
  33. {
  34. std::cout << "lambda reader: " << x << "\n";
  35. return 0;
  36. },
  37. [=](uint8_t*, size_t)
  38. {
  39. std::cout << "lambda writer: " << x << "\n";
  40. return 0;
  41. });
  42.  
  43. pc1.reader(nullptr, 0);
  44. pc1.writer(nullptr, 0);
  45.  
  46. pc2.reader(nullptr, 0);
  47. pc2.writer(nullptr, 0);
  48. protocol_callbacks pc3(bad_writer, reader);
  49. }
  50.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from prog.cpp:2:0:
/usr/include/c++/4.7/functional: In instantiation of ‘static _Res std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Res = int; _Functor = int (*)(short unsigned int*, unsigned int); _ArgTypes = {unsigned char*, unsigned int}]’:
/usr/include/c++/4.7/functional:2298:6:   required from ‘std::function<_Res(_ArgTypes ...)>::function(_Functor, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor = int (*)(short unsigned int*, unsigned int); _Res = int; _ArgTypes = {unsigned char*, unsigned int}; typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type = std::function<int(unsigned char*, unsigned int)>::_Useless]’
prog.cpp:48:46:   required from here
/usr/include/c++/4.7/functional:1912:40: error: cannot convert ‘unsigned char*’ to ‘short unsigned int*’ in argument passing
stdout
Standard output is empty