I have the following problem( see code below).
In a class that inherits from a base class, I want to be able to have two implementations of operator()
. One that takes integers, one that takes "indices", see simple Index
class. The operator(ints...)
will be in the child class, while I need to have the operator(Index...)
in the parent class (also with different return type.)
The code below is not the exact design but a minimal working example explaining the final problem.
Problem is, if I put operator(Index...)
in the child class, everything is good. If I put it in the base class, i get a compilation error:
error: no match for call to ‘(Tensor<1, int>) (Index<'i'>&)’
when I call vec(i) and the end of the main. I understand the compiler doesn't find the good method, but why ? Is there some "shadowing" rule associated to variadic templates ?
Thanks !
#include <iostream>
#include <type_traits>
template<char i>
class Index{
public:
Index(){};
};
template<int order, typename T, class tensor_type>
class Tensor_traits{
public:
//// here, doesn't compile ! !
//template <char i>
//T&operator()(Index<i> &ii){
//std::cout << "puet" << std::endl;
//}
};
template<int order, typename T>
class Tensor : public Tensor_traits<order, T, Tensor<order, T>> {
private:
int data[3] = {1,2,3};
public:
Tensor(){};
template <typename... Idx>
typename std::enable_if<std::is_same<Idx...,int>::value
or std::is_same<Idx...,unsigned int>::value
or std::is_same<Idx...,size_t>::value, T&>::type
operator()(const Idx&... idx){
return data[0]; //dummy here, actually i do other stuff !
}
// here, works!
template <char i>
T&operator()(Index<i> &ii){
std::cout << "puet" << std::endl;
}
};
int main() {
Tensor<1,int> vec1;
std::cout<< vec1(1) << std::endl;;
Index<'i'> i;
vec1(i);
}
Aucun commentaire:
Enregistrer un commentaire