fork(2) download
  1. #include <array>
  2. #include <cassert>
  3. #include <cstddef>
  4. #include <functional>
  5.  
  6. template <std::size_t N>
  7. class BaseClass {
  8. public:
  9. virtual std::array<int, N> CustomBehavior(const std::array<int, N>& user_array, int user_number) = 0;
  10. protected:
  11. std::array<int, N> my_array = {0, 0};
  12. };
  13.  
  14. template <std::size_t N>
  15. class DerivedClass : public BaseClass<N> {
  16. public:
  17. DerivedClass() = default;
  18.  
  19. DerivedClass(std::function<std::array<int, N>(const std::array<int, N>&, int)> custom_f)
  20. : customCallback(std::bind(custom_f, std::ref(std::placeholders::_1), std::placeholders::_2)) {}
  21.  
  22. void SetCustomBehavior(std::function<std::array<int, N>(const std::array<int>&, int)> custom_f) {
  23. customCallback = std::bind(custom_f, std::ref(std::placeholders::_1), std::placeholders::_2);
  24. }
  25.  
  26. std::array<int, N> CustomBehavior(const std::array<int, N>& user_array, int user_number) override {
  27. if (customCallback)
  28. this->my_array = customCallback(user_array, user_number);
  29. return this->my_array;
  30. }
  31.  
  32. private:
  33. std::function<std::array<int, N>(const std::array<int, N>&, int)> customCallback;
  34. };
  35.  
  36. static constexpr std::size_t MySize = 2;
  37.  
  38. std::array<int, MySize> my_behavior(const std::array<int, MySize>& input_array, int a) {
  39. return {a * input_array[0], a * input_array[1]};
  40. }
  41.  
  42. int main() {
  43.  
  44. std::array<int, MySize> my_array = {1, 1};
  45.  
  46. // Default constructor
  47. DerivedClass<MySize> foo_1; // OK
  48. std::array<int, MySize> bar_1 = foo_1.CustomBehavior(my_array, 2);
  49. assert(bar_1[0] == 2 && bar_1[1] == 2);
  50.  
  51. // Custom constructor
  52. DerivedClass<MySize> foo_2(my_behavior); // COMPILATION ERROR
  53. std::array<int, MySize> bar_2 = foo_2.CustomBehavior(my_array, 2);
  54. assert(bar_1[0] == 2 && bar_1[1] == 2);
  55.  
  56. // Default constructor with custom behavior set later on
  57. DerivedClass<MySize> foo_3; // OK
  58. foo_3.SetCustomBehavior(my_behavior); // COMPILATION ERROR
  59. std::array<int, MySize> bar_3 = foo_3.CustomBehavior(my_array, 2);
  60. assert(bar_1[0] == 2 && bar_1[1] == 2);
  61.  
  62. return 0;
  63. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:22:81: error: wrong number of template arguments (1, should be 2)
     void SetCustomBehavior(std::function<std::array<int, N>(const std::array<int>&, int)> custom_f) {
                                                                                 ^
In file included from prog.cpp:1:
/usr/include/c++/8/array:94:12: note: provided for ‘template<class _Tp, long unsigned int _Nm> struct std::array’
     struct array
            ^~~~~
prog.cpp: In function ‘int main()’:
prog.cpp:58:40: error: no matching function for call to ‘DerivedClass<2>::SetCustomBehavior(std::array<int, 2> (&)(const std::array<int, 2>&, int))’
     foo_3.SetCustomBehavior(my_behavior); // COMPILATION ERROR
                                        ^
prog.cpp:22:10: note: candidate: ‘void DerivedClass<N>::SetCustomBehavior(std::function<std::array<int, N>(const int&, int)>) [with long unsigned int N = 2]’
     void SetCustomBehavior(std::function<std::array<int, N>(const std::array<int>&, int)> custom_f) {
          ^~~~~~~~~~~~~~~~~
prog.cpp:22:10: note:   no known conversion for argument 1 from ‘std::array<int, 2>(const std::array<int, 2>&, int)’ to ‘std::function<std::array<int, 2>(const int&, int)>’
prog.cpp: In instantiation of ‘DerivedClass<N>::DerivedClass(std::function<std::array<int, N>(const std::array<int, N>&, int)>) [with long unsigned int N = 2]’:
prog.cpp:52:43:   required from here
prog.cpp:20:101: error: no matching function for call to ‘std::function<std::array<int, 2>(const std::array<int, 2>&, int)>::function(std::_Bind_helper<false, std::function<std::array<int, 2>(const std::array<int, 2>&, int)>&, std::reference_wrapper<const std::_Placeholder<1> >, const std::_Placeholder<2>&>::type)’
         : customCallback(std::bind(custom_f, std::ref(std::placeholders::_1), std::placeholders::_2)) {}
                                                                                                     ^
In file included from /usr/include/c++/8/functional:59,
                 from prog.cpp:4:
/usr/include/c++/8/bits/std_function.h:446:2: note: candidate: ‘template<class _Functor, class, class> std::function<_Res(_ArgTypes ...)>::function(_Functor)’
  function(_Functor);
  ^~~~~~~~
/usr/include/c++/8/bits/std_function.h:446:2: note:   template argument deduction/substitution failed:
/usr/include/c++/8/bits/std_function.h:445:9: error: no type named ‘type’ in ‘class std::result_of<std::_Bind<std::function<std::array<int, 2>(const std::array<int, 2>&, int)>(std::reference_wrapper<const std::_Placeholder<1> >, std::_Placeholder<2>)>&(const std::array<int, 2>&, int)>’
         typename = _Requires<_Callable<_Functor>, void>>
         ^~~~~~~~
/usr/include/c++/8/bits/std_function.h:422:7: note: candidate: ‘std::function<_Res(_ArgTypes ...)>::function(std::function<_Res(_ArgTypes ...)>&&) [with _Res = std::array<int, 2>; _ArgTypes = {const std::array<int, 2>&, int}]’
       function(function&& __x) noexcept : _Function_base()
       ^~~~~~~~
/usr/include/c++/8/bits/std_function.h:422:7: note:   no known conversion for argument 1 from ‘std::_Bind_helper<false, std::function<std::array<int, 2>(const std::array<int, 2>&, int)>&, std::reference_wrapper<const std::_Placeholder<1> >, const std::_Placeholder<2>&>::type’ {aka ‘std::_Bind<std::function<std::array<int, 2>(const std::array<int, 2>&, int)>(std::reference_wrapper<const std::_Placeholder<1> >, std::_Placeholder<2>)>’} to ‘std::function<std::array<int, 2>(const std::array<int, 2>&, int)>&&’
/usr/include/c++/8/bits/std_function.h:652:5: note: candidate: ‘std::function<_Res(_ArgTypes ...)>::function(const std::function<_Res(_ArgTypes ...)>&) [with _Res = std::array<int, 2>; _ArgTypes = {const std::array<int, 2>&, int}]’
     function<_Res(_ArgTypes...)>::
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/8/bits/std_function.h:652:5: note:   no known conversion for argument 1 from ‘std::_Bind_helper<false, std::function<std::array<int, 2>(const std::array<int, 2>&, int)>&, std::reference_wrapper<const std::_Placeholder<1> >, const std::_Placeholder<2>&>::type’ {aka ‘std::_Bind<std::function<std::array<int, 2>(const std::array<int, 2>&, int)>(std::reference_wrapper<const std::_Placeholder<1> >, std::_Placeholder<2>)>’} to ‘const std::function<std::array<int, 2>(const std::array<int, 2>&, int)>&’
/usr/include/c++/8/bits/std_function.h:402:7: note: candidate: ‘std::function<_Res(_ArgTypes ...)>::function(std::nullptr_t) [with _Res = std::array<int, 2>; _ArgTypes = {const std::array<int, 2>&, int}; std::nullptr_t = std::nullptr_t]’
       function(nullptr_t) noexcept
       ^~~~~~~~
/usr/include/c++/8/bits/std_function.h:402:7: note:   no known conversion for argument 1 from ‘std::_Bind_helper<false, std::function<std::array<int, 2>(const std::array<int, 2>&, int)>&, std::reference_wrapper<const std::_Placeholder<1> >, const std::_Placeholder<2>&>::type’ {aka ‘std::_Bind<std::function<std::array<int, 2>(const std::array<int, 2>&, int)>(std::reference_wrapper<const std::_Placeholder<1> >, std::_Placeholder<2>)>’} to ‘std::nullptr_t’
/usr/include/c++/8/bits/std_function.h:395:7: note: candidate: ‘std::function<_Res(_ArgTypes ...)>::function() [with _Res = std::array<int, 2>; _ArgTypes = {const std::array<int, 2>&, int}]’
       function() noexcept
       ^~~~~~~~
/usr/include/c++/8/bits/std_function.h:395:7: note:   candidate expects 0 arguments, 1 provided
stdout
Standard output is empty