I have two datatypes called DragonVector
and UnbiasedDragon
and I am using visitor pattern for dynamic type inference.
I want to extend a DragonVector
only by a DragonVector
and similarly for UnbiasedDragon
.
I have the following code for extending the vectors:
template<class T>
class ExtendVisitor{
public:
void operator()(DragonVector<T>& vec1, const DragonVector<T>& vec2){
vec1.extend(vec2);
}
void operator()(UnbiasedDragon<T>& vec1, const UnbiasedDragon<T>& vec2){
vec1.extend(vec2);
}
void operator()(auto& vec1, const auto& vec2){
std::cout<<"wrong class"<<std::endl;
}
};
I get error: 'auto' not allowed in function prototype
. I am using C++17.
Since, there are only two classes, I can exhaustively write the operator overloads in the visitor for all the combinations. But this seems infeasible as the number of classes grow large.
I tried using templating as a work around as
template<class T>
class ExtendVisitor{
public:
void operator()(DragonVector<T>& vec1, const DragonVector<T>& vec2){
vec1.extend(vec2);
}
void operator()(UnbiasedDragon<T>& vec1, const UnbiasedDragon<T>& vec2){
vec1.extend(vec2);
}
template<class TT>
void operator()(TT& vec1, const TT& vec2){
std::cout<<"wrong class"<<std::endl;
}
};
but this also did not work out.
Is there a way to use visitor pattern without having to write all the possible combinations?
constexpr
detector that returnstrue
forDragonVector<T>
,UnbiasedDragon<T>
, ... andfalse
otherwise, then usestd::enable_if<>
.