how to use a base class template function in a derived case?
Example:
Class A{
protected:
template<typename... Args>
std::vector<torch::Tensor> generate_value(const size_t& generated_points, std::function<int(const Args&...)> function, const Args&... args);
};
class B : protected A{
public:
B() : A(){}
protected:
std::vector<torch::Tensor> generate_values(const size_t& generated_points, const size_t& column){
return this->generate_values(generated_points, std::bind(&A::inner_generation, this, std::placeholders::_1), column);
}
<torch::Tensor inner_generation(const size_t& column);
};
Compiling use Clang I get the following error:
error: no matching member function for call to 'generate_values'
return this->generate_values(generated_points, function, column);
~~~~~~^~~~~~~~~~~~~~~
note: candidate template ignored: could not match 'std::function<torch::Tensor (const Args &...)>
std::vector<torch::Tensor> generate_values(const size_t& generated_points, std::function<torch::Tensor(const Args&...)> function, const Args&... args)
I am not sure what is the issue.
I hope someone could guide me.
Originally, I had the function with same name, it was only finding the one in class B. I have then changed the names it is still not found. I am not sure is that due to the template.
std::functionis rather expensive in usage and little flexible – if you go along with yet another template parameter you are more efficient, more flexible and even get simpler code:Though if using lambdas (or
std::bindas well, if you bind the values directly instead of using place holders) you don't even need additional parameters:Which one to choose? Well, a matter of personal taste mainly – unless you want to do the same multiple times with different variables, then the former variant is of advantage for not having to create multiple lambdas:
This latter example might not be meaningful in your concrete case, but might come into play in other scenarios ;)
Side note: Just for explanation: The original variant fails for neither the result of
bindnor a lambda actually being astd::functionobject, thus not matching your template parameter. You can solve by creating thestd::functionobject explicitly:Note, too, that in the original code of the question the return types of the
std::functioninstantiations do not match either (firstint, thentorch::Tensor– unlesstorch::Tensorhappens to be atypedefforint…).