Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to recognize type
QtWS25 Last Chance

How to recognize type

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 2.8k 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.
  • P Offline
    P Offline
    pmjz
    wrote on last edited by
    #1

    hi guys
    @template<class T>
    static bool lessOrMoreThan(const T &, const T &,int lessOrMoreFlag)
    {
    if(lessOrMoreFlag == 0){
    ;
    }
    if(lessOrMoreFlag == 1){
    ;
    }
    }@

    if I pass QString or int in this template function, How can I know the exact type I passed in? thanks any comments

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      What do you mean exactly? Can you elaborate?

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pmjz
        wrote on last edited by
        #3

        @template<class T>
        static bool lessOrMoreThan(const T &, const T &,int lessOrMoreFlag)
        {
        if(lessOrMoreFlag == 0){
        if(T == QString);
        {
        do something;
        }
        if(T == int)
        {
        do something;
        }else{
        do something
        }
        }
        if(lessOrMoreFlag == 1){
        ;
        }
        }@

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pmjz
          wrote on last edited by
          #4

          thank you for your interest, I post a piece of new code above.any comments

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kxyu
            wrote on last edited by
            #5

            Do you actually need templates? May be you can try just overloading for types you need

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pmjz
              wrote on last edited by
              #6

              [quote author="Kxyu" date="1321640229"]Do you actually need templates? May be you can try just overloading for types you need[/quote]

              that is a good way, but too much.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                If you need to know the type of the template in a method, this is a strong sign of wrong design and that you most probably shouldn't use a template at all.

                Can you describe what you want to achieve? Maybe we can come up with a better suitable and less problematic idea.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  pmjz
                  wrote on last edited by
                  #8

                  you are right. I use other way and fix my problem already.anyway,thanks guys

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    veeeee_d
                    wrote on last edited by
                    #9

                    You can specialize your template function to your desired types. This is a standard feature of C++, you can find it under "Specialized Template Functions" or something like that.

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      Kxyu
                      wrote on last edited by
                      #10

                      Yeah, you're right! It's awkward that I've missed that on Stroustrup. So you can just define your template function and then overload it for types that need some special action.

                      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