I am attempting to make a generic class, which conditionally calls a function based on the number of parameters supplied to the class. I've been using std::enable_if, but to no avail.
I have tried a variety of different things using three functions (for 2 optional class parameters). I have read many different posts here on stack overflow, which is how I got to what I am currently using, however I am still getting errors.
#include <qsqldatabase.h>
template<class EditorDialog, typename FirstOpt = void, typename SecondOpt = void>
class GenericItemDelegate : public QItemDelegate {
public:
explicit GenericItemDelegate( QSqlDatabase &connection, QObject *parent = 0 ) : QItemDelegate( parent ), connection( connection ) {}
protected:
QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override {
return createEditorHidden( parent );
}
void setEditorData( QWidget *editor, const QModelIndex &index ) const override {
EditorDialog *dialog = static_cast<EditorDialog *>( editor );
dialog->loadData( index.data( Qt::ItemDataRole::UserRole ) );
}
void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override {
EditorDialog *dialog = static_cast<EditorDialog *>( editor );
if ( dialog->result() == QDialog::Accepted ) {
model->setData( index, QVariant::fromValue( dialog->getData() ) );
}
}
private:
template<typename f = FirstOpt, typename s = SecondOpt, typename std::enable_if<std::is_void<f>::value && std::is_void<s>::value, QWidget *>::type = true>
QWidget *createEditorHidden( QWidget *parent ) const {
return new EditorDialog( connection, parent );
}
template<typename f = FirstOpt, typename s = SecondOpt, typename std::enable_if<!std::is_void<f>::value && !std::is_void<s>::value, QWidget *>::type = true>
QWidget * createEditorHidden( QWidget * parent ) const {
return new EditorDialog( connection, parent, FirstOpt );
}
template<typename f = FirstOpt, typename s = SecondOpt, typename std::enable_if<!std::is_void<f>::value && !std::is_void<s>::value, QWidget *>::type = true>
QWidget * createEditorHidden( QWidget * parent ) const {
return new EditorDialog( connection, parent, FirstOpt, SecondOpt );
}
QSqlDatabase connection;
};
What I am currently getting with the above is error C2672: 'GenericItemDelegate<CreateFormulaDialog,void,void>::createEditorHidden': no matching overloaded function found
which does not make sense to me, since (in this case) both template parameters are void.
Effectively, in the end, I would want objects created with GenericItemDelegate<CreateFormulaDialog>
to call the first template function, GenericItemDelegate<CreateFormulaDialog, true>
to call the second template function, and GenericItemDelegate<CreateFormulaDialog,true,false>
to call the third.
Hopefully I'm making sense. I've spent the last few hours working on this, and my brain is just not working anymore.