Is 'deep' template argument deduction posible?

Viewed 77

The point is, that I have template function, and I want to use it as callback:

template <class ...Ind>
void fill_given_rows(mat& matrix, Ind ...ind){

It calls from a class member function like that:

template<typename F, class ... Args>
void pic::modify(F func, Args ... args){
  func(matrix, args...);
}

which one in its order calls from main as:

matrix.modify(fill_given_rows, 0, 2, 3);

gcc gives me such an output:

50:41: error: no matching function for call to 'pic::modify(<unresolved overloaded function type>, int, int, int)'
50:41: note: candidate is:
27:8: note: template<class F, class ... Args> void pic::modify(F, Args ...)
27:8: note:   template argument deduction/substitution failed:
50:41: note:   couldn't deduce template parameter 'F'

Here is full version of code:

#include <vector>
#include <array>
#include <initializer_list>
#include <type_traits>

typedef std::vector<std::vector<int>> mat;

template <class ...Ind>
void fill_given_rows(mat& matrix, Ind ...ind){
  std::array<std::common_type_t<Ind...>, sizeof...(Ind)> lines = {{ind...}};
  int height = matrix.size();
  int width = matrix[0].size();

  for(auto row: lines){
    for(int y=0; y<width; y++){
        matrix[row][y]=1;
    }
  }
}

class pic{
public:
  pic(int width, int height); //generate empty matrix

  //callback for all matrix modification 
  template<typename F, class ... Args>
  void modify(F, Args ...); 

private:
  mat matrix;
};

pic::pic(int width, int height){
  matrix.resize(height);
  for(auto& row: matrix){
    row.resize(width);
  }
}

template<typename F, class ... Args>
void pic::modify(F func, Args ... args){
  func(matrix, args...);
}


int main() {
  int width=10, height=5;
  pic matrix(width, height);

  matrix.modify(fill_given_rows, 0, 2, 3);

  return 0;
}

Why it does not work?

1 Answers

Maybe if you use a functional instead of a function template?

Something as

struct foo
 {
   template <typename ... Ind>
   void operator() (mat & matrix, Ind ...ind) {
      std::array<std::common_type_t<Ind...>, sizeof...(Ind)> lines = {{ind...}};
      int height = matrix.size();
      int width = matrix[0].size();

      for(auto row: lines){
         for(int y=0; y<width; y++){
            matrix[row][y]=1;
         }
      }
   }
 };

// ...

foo f;

matrix.modify(f, 0, 2, 3);

The problem with your code

// ...........vvvvvvvvvvvvvvv  template function              
matrix.modify(fill_given_rows, 0, 2, 3);

is that a function template isn't an object but a set of objects.

And you can't pass a set of objects as an argument of a function.

You can select the correct object, from the set, explicating the template types

// ...........vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv  now is a single object              
matrix.modify(fill_given_rows<int, int, int>, 0, 2, 3);

but (IMHO) this is a little uncomfortable.

Passing a functional (with a template operator()) you pass a single object that "contains" a function template.

Related