Answer the question
In order to leave comments, you need to log in
How to learn to write relatively universal code using templates?
Now, I'm working on my own, small, one might say educational project, in which I actively began to use template methods. For example:
Внутри `OpenFile` - вызывается шаблонный метод `readData`:
template<typename retT,typename containerCastClass,typename castedClassType, typename obj_class, typename obj, typename ...args>
static retT readData( containerCastClass c,castedClassType c1,obj_class cl, obj o,args... ar);
Пример его вызова:
readData<QStringList>(static_cast<QStringList(QVariant::*)() const>(&QVariant::toStringList),
static_cast<void(QStringList::*)(const QList<QString> &t)>(&QStringList::append),
static_cast< QByteArray ( QIODevice:: * )(qint64 maxSize) >(&QFile::readLine),
currentOpenTxt,
qint64{0});
Собственно, основная работа метода, не учитывая дополнительных проверок:
auto read{
[&]()->auto{
static auto castedContainer = std::invoke(c, currentContainer);// convert QVariant to QStringList, QMap..
qDebug()<<typeid (castedContainer).raw_name();
QString readedData=std::invoke(cl,o,ar...); //read line from txt, row/column from xlsx
std::invoke(c1,castedContainer,retT({readedData}));//append, insert...
return castedContainer;
}
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question