fork(1) download
  1. #include <iostream>
  2. #include <utility>
  3. #include <functional>
  4.  
  5. using namespace std;
  6.  
  7. // -----------------------------------------------
  8. template<typename T, typename F>
  9. struct Test
  10. {
  11. T m_resource;
  12. F m_deleter;
  13.  
  14. Test(T&& resource, F&& deleter)
  15. : m_resource(move(resource)), m_deleter(move(deleter))
  16. {
  17. }
  18. Test(T const& resource, F const& deleter)
  19. : m_resource(resource), m_deleter(deleter)
  20. {
  21. }
  22. };
  23. // -----------------------------------------------
  24.  
  25. // -----------------------------------------------
  26. template<typename T, typename F>
  27. Test<T, F> test(T&& t, F&& f)
  28. {
  29. return Test<T, F>(move(t), move(f));
  30. }
  31. template<typename T, typename F>
  32. Test<T, F> test(T const& t, F const& f)
  33. {
  34. return Test<T, F>(t, f);
  35. }
  36. // -----------------------------------------------
  37.  
  38. int main()
  39. {
  40. // construct from temporaries --------------------
  41. Test<int*, function<void(int*)>> t(new int, [](int *k) {}); // OK - constructor
  42. auto tm = test(new int, [](int *k){}); // OK - maker function
  43. // -----------------------------------------------
  44.  
  45. // construct from l-values -----------------------
  46. int *N = new int(24);
  47. auto F = function<void(int*)>([](int *k){});
  48.  
  49. Test<int*, function<void(int*)>> tt(N, F); // OK - construction
  50. auto m = test(N, F); // Error - maker function
  51. // -----------------------------------------------
  52.  
  53. return 0;
  54. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘struct Test<int*&, std::function<void(int*)>&>’:
prog.cpp:50:20:   required from here
prog.cpp:18:2: error: ‘Test<T, F>::Test(const T&, const F&) [with T = int*&; F = std::function<void(int*)>&]’ cannot be overloaded
  Test(T const& resource, F const& deleter) 
  ^
prog.cpp:14:2: error: with ‘Test<T, F>::Test(T&&, F&&) [with T = int*&; F = std::function<void(int*)>&]’
  Test(T&& resource, F&& deleter) 
  ^
prog.cpp: In instantiation of ‘Test<T, F> test(T&&, F&&) [with T = int*&; F = std::function<void(int*)>&]’:
prog.cpp:50:20:   required from here
prog.cpp:29:36: error: no matching function for call to ‘Test<int*&, std::function<void(int*)>&>::Test(std::remove_reference<int*&>::type, std::remove_reference<std::function<void(int*)>&>::type)’
  return Test<T, F>(move(t), move(f));
                                    ^
prog.cpp:29:36: note: candidates are:
prog.cpp:14:2: note: Test<T, F>::Test(T&&, F&&) [with T = int*&; F = std::function<void(int*)>&]
  Test(T&& resource, F&& deleter) 
  ^
prog.cpp:14:2: note:   no known conversion for argument 1 from ‘std::remove_reference<int*&>::type {aka int*}’ to ‘int*&’
prog.cpp:9:8: note: constexpr Test<int*&, std::function<void(int*)>&>::Test(const Test<int*&, std::function<void(int*)>&>&)
 struct Test
        ^
prog.cpp:9:8: note:   candidate expects 1 argument, 2 provided
prog.cpp:9:8: note: constexpr Test<int*&, std::function<void(int*)>&>::Test(Test<int*&, std::function<void(int*)>&>&&)
prog.cpp:9:8: note:   candidate expects 1 argument, 2 provided
prog.cpp: In function ‘Test<T, F> test(T&&, F&&) [with T = int*&; F = std::function<void(int*)>&]’:
prog.cpp:30:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
stdout
Standard output is empty