I am wondering whether it is possible to have an operator in a non-templated parent class with an auto
return value and a templated child class??
Here is an example:
#include <vector>
#include <iostream>
class parent{
public:
virtual ~parent(){};
auto & operator()(const int i);
};
template<typename T>
class child:public parent{
protected:
std::vector<T> X_;
public:
child(std::vector<T> const &X):X_{X}{};
T & operator()(const int i){
return(X_[i]);
}
};
auto & parent::operator()(const int i){
if(auto * x=dynamic_cast<child<int>*>(this)){
return((*x)(i));
}else if(auto * x=dynamic_cast<child<double>*>(this)){
return((*x)(i));
}else{
throw std::string("error");
}
};
int main(){
child Y1(std::vector<int>{5,10});
child Y2(std::vector<double>{6,11});
std::cout<<Y1(0)<<std::endl;
parent * X=&Y1;
std::cout<<(*X)(0)<<std::endl;
}
Currently I get: error: 'auto' in return type deduced as 'double' here but deduced as 'int' in earlier return statement
Any suggestions much appreciated.