Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Templates

Templates

Scheduled Pinned Locked Moved C / C++ / MFC
wpfquestion
14 Posts 5 Posters 0 Views 1 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.
  • R Russell

    Hi, Is it possible to use tests on the template typename(during compile, not in runtime)? Something like: template<typename T> class MyClass{ ... T m_Var; ... void fun(){ #if T==double .... #else .. #endif } } Cheers, Russell

    O Offline
    O Offline
    Oliver Anhuth
    wrote on last edited by
    #3

    template<typename T> class MyClass; template<> class MyClass<double> { ... T m_Var; ... void fun(){ // your double work } }; template<> class MyClass<int> { ... T m_Var; ... void fun() { // your int work } };

    -- modified at 4:08 Thursday 20th July, 2006

    1 Reply Last reply
    0
    • T toxcct

      no, because T is assigned at runtime, while #if is evaluated before the compilation BTW, there's a mechanism called templates specializations, which allow you to design the templated class for a particuliar template parameter...


      TOXCCT >>> GEII power

      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

      R Offline
      R Offline
      Russell
      wrote on last edited by
      #4

      toxcct wrote:

      T is assigned at runtime

      I'm not sure, are you? templates are expanded during compilation. Elseway have you got a good link to learn something on "templates specializations". Thanks. Cheers, Russell

      T 1 Reply Last reply
      0
      • R Russell

        toxcct wrote:

        T is assigned at runtime

        I'm not sure, are you? templates are expanded during compilation. Elseway have you got a good link to learn something on "templates specializations". Thanks. Cheers, Russell

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #5

        hum, ok, maybe not at runtime, but at compile time at least. unfortunately, preprocessing directives are executed BEFORE the compilation... so, no chance to do it like this


        TOXCCT >>> GEII power

        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

        R 1 Reply Last reply
        0
        • T toxcct

          hum, ok, maybe not at runtime, but at compile time at least. unfortunately, preprocessing directives are executed BEFORE the compilation... so, no chance to do it like this


          TOXCCT >>> GEII power

          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

          R Offline
          R Offline
          Russell
          wrote on last edited by
          #6

          yes, I know that I can't use preprocessing directives, It was only an sintax example: I'm asking exacly on the template sintax that it can be used. Cheers, Russell

          T 1 Reply Last reply
          0
          • R Russell

            yes, I know that I can't use preprocessing directives, It was only an sintax example: I'm asking exacly on the template sintax that it can be used. Cheers, Russell

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #7

            then search for "template specializations"


            TOXCCT >>> GEII power

            [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

            1 Reply Last reply
            0
            • T toxcct

              no, because T is assigned at runtime, while #if is evaluated before the compilation BTW, there's a mechanism called templates specializations, which allow you to design the templated class for a particuliar template parameter...


              TOXCCT >>> GEII power

              [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

              N Offline
              N Offline
              NrmMyth
              wrote on last edited by
              #8

              toxcct wrote:

              because T is assigned at runtime

              Templates ane not .NET generics. Compiler generates all specializations for used parameters. template class CLS; CLS aaa; CLS > bbb; At compile time, this will create double and vector specializations for template.

              T 1 Reply Last reply
              0
              • N NrmMyth

                toxcct wrote:

                because T is assigned at runtime

                Templates ane not .NET generics. Compiler generates all specializations for used parameters. template class CLS; CLS aaa; CLS > bbb; At compile time, this will create double and vector specializations for template.

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #9

                WTF :omg::wtf: what are you talking about ?

                NrmMyth wrote:

                Templates ane not .NET generics.

                where have you seen me saying this ? and what is the point in saying this to me ? do you criticise my answer ? do you suggest something better than what i did ? :~ :suss:


                TOXCCT >>> GEII power

                [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                N 1 Reply Last reply
                0
                • T toxcct

                  WTF :omg::wtf: what are you talking about ?

                  NrmMyth wrote:

                  Templates ane not .NET generics.

                  where have you seen me saying this ? and what is the point in saying this to me ? do you criticise my answer ? do you suggest something better than what i did ? :~ :suss:


                  TOXCCT >>> GEII power

                  [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                  N Offline
                  N Offline
                  NrmMyth
                  wrote on last edited by
                  #10

                  You gave an wrong information. I think that everyone has a right to correct you.

                  T 1 Reply Last reply
                  0
                  • N NrmMyth

                    You gave an wrong information. I think that everyone has a right to correct you.

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #11

                    NrmMyth wrote:

                    I think that everyone has a right to correct you.

                    of course ! but not only he doesn't explain where i was wrong but he also "repeats" words that i never said ! :( where have you heard me saying that templates were .NET specifics ? :mad:


                    TOXCCT >>> GEII power

                    [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                    N 1 Reply Last reply
                    0
                    • T toxcct

                      NrmMyth wrote:

                      I think that everyone has a right to correct you.

                      of course ! but not only he doesn't explain where i was wrong but he also "repeats" words that i never said ! :( where have you heard me saying that templates were .NET specifics ? :mad:


                      TOXCCT >>> GEII power

                      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                      N Offline
                      N Offline
                      NrmMyth
                      wrote on last edited by
                      #12

                      I aplogize for every mental pain I caused. Please accept it.

                      T 1 Reply Last reply
                      0
                      • N NrmMyth

                        I aplogize for every mental pain I caused. Please accept it.

                        T Offline
                        T Offline
                        toxcct
                        wrote on last edited by
                        #13

                        nevermind


                        TOXCCT >>> GEII power

                        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                        1 Reply Last reply
                        0
                        • R Russell

                          Hi, Is it possible to use tests on the template typename(during compile, not in runtime)? Something like: template<typename T> class MyClass{ ... T m_Var; ... void fun(){ #if T==double .... #else .. #endif } } Cheers, Russell

                          M Offline
                          M Offline
                          Michael Dunn
                          wrote on last edited by
                          #14

                          http://www.parashift.com/c++-faq-lite/templates.html#faq-35.7

                          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups