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. check template type for type
Forum Update on Monday, May 27th 2025

check template type for type

Scheduled Pinned Locked Moved C++ Gurus
template
6 Posts 5 Posters 5.0k Views
  • 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.
  • T Offline
    T Offline
    themts
    wrote on last edited by
    #1

    Hey guys,

    I know, it's probably more a general c++ question than qt but anyway...

    I have a dll which provides functions like:

    bool GetDataAsU8(unsigned char *data)
    bool GetDataAsS16(short *data)
    bool GetDataAsU32(uint *data)
    ...
    

    I want to write a helper function which uses templates and selects the right imported dll function automatically.

    template <typename T> bool GetData(T *data)
    {
        //here I want to check the type of data and select the right function
    }
    

    Could someone help?
    Thanks in advance!

    mts

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi,
      why can't you simply provide several overloaded versions of GetData() ?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mcosta
        wrote on last edited by
        #3

        You could use the typeid operator

        if (typeid(data) == typeid(uint)) {
        ...
        } 
        

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          If you only want one function you could use the technique described here to "if" trough possible types.

          Otherwise, template specialization could also be an option

          Hope it helps

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            To check the type at runtime you can also use is_same template:

            #include <type_traits>
            template <typename T> bool GetData(T *data) {
                if(std::is_same<T, unsigned char>::value) return GetDataAsU8(data);
                //and so on for other types
            }
            

            but this is unnecessarily slow, so is the typeid solution.
            Template specialization, as suggested by @SGaist, is the way to go here.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              themts
              wrote on last edited by
              #6

              Thanks for your answers!
              I think I will go with Template specialization.

              CU
              mts

              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