Solved new type in template function
-
Hi,
I have 2 structures:
class MainWindow : public QMainWindow { Q_OBJECT public: typedef struct { wchar_t a[256]; wchar_t b[256]; wchar_t c[256]; } ABC; typedef struct { wchar_t a[256]; wchar_t d[512]; } AD; ............ } Q_DECLARE_METATYPE(MainWindow::ABC); Q_DECLARE_METATYPE(MainWindow::AD);
I create two QVectors:
QVector<MainWindow::ABC> vectorAbc; QVector<MainWindow::AD> vectorAd;
and add some data to this vectors.
Next I register types:
int idABC = qRegisterMetaType<MainWindow::ABC>("ABC"); int idAD = qRegisterMetaType<MainWindow::AD>("AD");
And now I would like to send them to function, so I create:
template <class type> void MainWindow::updateVector(QVector<type> vector) { }
In this function (updateVector) I want do something like:
template <class type> void MainWindow::updateVector(QVector<type> vector) { if(type == "ABC" ) // of course I don't have to compare type with text; I can compare type with idABC { for(int i=0; i<vector.size();i++) { vector[i].A = someValue; vector[i].B = someValue; vector[i].C = someValue; } } else { for(int i=0; i<vector.size();i++) { vector[i].A = someValue; vector[i].D = someValue; } } }
[Moved to C++ Gurus ~kshegunov]
-
What is your question?
-
if constexpr (std::is_same<type, MainWindow::ABC>::value) { ... } else { ... }
Or alternatively SFINAE out the type and provide two separate overloads:
template <class type> typename std::enable_if<std::is_same<type, MainWindow::ABC>::value>::type MainWindow::updateVector(QVector<type> vector) { } template <class type> typename std::enable_if<std::is_same<type, MainWindow::AD>::value>::type MainWindow::updateVector(QVector<type> vector) { }
-
How to do this?
Of course I can't compare type with "ABC". This is only pseudocode, which doesn't work.
-
@kshegunov Perfect!