fork(3) download
  1. #include <utility>
  2.  
  3. template<typename F>
  4. class A {
  5. public:
  6. A(const F& f) : _f(f) {}
  7. A(F&& f) : _f(std::move(f)) {}
  8.  
  9. private:
  10. F _f;
  11. };
  12.  
  13. template<typename F>
  14. A<F> MakeA(F&& f) {
  15. return A<F>(std::forward<F>(f));
  16. }
  17.  
  18. void foo() {}
  19.  
  20. int main() {
  21. auto a = MakeA([]{}); // OK
  22. auto b = MakeA(&foo); // OK
  23. auto c = MakeA(foo); // <-- Error with overloading
  24. }
Compilation error #stdin compilation error #stdout 0s 3292KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘class A<void (&)()>’:
prog.cpp:23:20:   required from here
prog.cpp:7:3: error: ‘A<F>::A(F&&) [with F = void (&)()]’ cannot be overloaded
   A(F&& f) : _f(std::move(f)) {}
   ^
prog.cpp:6:3: error: with ‘A<F>::A(const F&) [with F = void (&)()]’
   A(const F& f) : _f(f) {}
   ^
stdout
Standard output is empty