Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. new type in template function
Forum Updated to NodeBB v4.3 + New Features

new type in template function

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 3 Posters 643 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qwe3
    wrote on last edited by kshegunov
    #1

    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]

    kshegunovK 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      What is your question?

      (Z(:^

      1 Reply Last reply
      0
      • Q qwe3

        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]

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3
        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)
        {
        }
        

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        5
        • Q Offline
          Q Offline
          qwe3
          wrote on last edited by
          #4

          How to do this?

          Of course I can't compare type with "ABC". This is only pseudocode, which doesn't work.

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qwe3
            wrote on last edited by
            #5

            @kshegunov Perfect!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved