fork download
  1. #include <string>
  2. #include <functional>
  3. #include <iostream>
  4.  
  5. template<class T>
  6. struct reference_only {
  7. T& t;
  8. operator T&(){ return t; }
  9. operator T()=delete;
  10. reference_only(T& tin):t(tin){}
  11. };
  12.  
  13. struct Foo {};
  14. typedef void func_type(Foo *&);
  15. typedef std::function<void(reference_only<Foo*>)> FunctionalType;
  16.  
  17. void foo1(Foo *)
  18. {
  19. std::cout << "foo1\n";
  20. }
  21. void foo2(Foo *&)
  22. {
  23. std::cout << "foo2\n";
  24. }
  25.  
  26. int main()
  27. {
  28. Foo f;
  29. Foo* pf = &f;
  30. #if 1
  31. FunctionalType f1 = foo1;
  32. f1(pf);
  33. #else
  34. FunctionalType f2 = foo2;
  35. f2(pf);
  36. #endif
  37. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:31:24: error: conversion from 'void(Foo*)' to non-scalar type 'FunctionalType {aka std::function<void(reference_only<Foo*>)>}' requested
    FunctionalType f1 = foo1;
                        ^
stdout
Standard output is empty