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. using template functions as part of a class? is it possible?

using template functions as part of a class? is it possible?

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionalgorithmshelp
7 Posts 3 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.
  • J Offline
    J Offline
    Joan M
    wrote on last edited by
    #1

    Hello, I need to use a template function, but I need that that template function be a class member... Let's say that I've tried to do:

    INSIDE THE HEADER FILE:
    template<class T> void MyClass::MyFunction(T& Parameter);

    INSIDE THE IMPLEMENTATION FILE:
    template<class T> void CMyClass::MyFunction(T& Parameter)
    {
    SomeOperations...
    }

    HOW DO I CALL THE FUNCTION:
    bool b = false;
    MyFunction(b);

    The fact is that using this code I get a lot of errors (one in each MyFunction call) like: ...MyFile.cpp(119) : error C2893: Failed to specialize function template 'void __thiscall CMyclass::MyFunction(T &)' With the following template arguments: 'bool' The strange thing is that if I remove the declaration from the class (I've commented it out) and I call the function as a non class member, then it executes well... I need to be able to call that function while that function would be a part of CMyClass. Is this possible? and if it is possible, what do I'm doing wrong? Thank you in advance. NOTE: Somebody has told me that this is a weakness of the VC++ compiler, but I continue searching because I need it, what do you believe?

    J J 2 Replies Last reply
    0
    • J Joan M

      Hello, I need to use a template function, but I need that that template function be a class member... Let's say that I've tried to do:

      INSIDE THE HEADER FILE:
      template<class T> void MyClass::MyFunction(T& Parameter);

      INSIDE THE IMPLEMENTATION FILE:
      template<class T> void CMyClass::MyFunction(T& Parameter)
      {
      SomeOperations...
      }

      HOW DO I CALL THE FUNCTION:
      bool b = false;
      MyFunction(b);

      The fact is that using this code I get a lot of errors (one in each MyFunction call) like: ...MyFile.cpp(119) : error C2893: Failed to specialize function template 'void __thiscall CMyclass::MyFunction(T &)' With the following template arguments: 'bool' The strange thing is that if I remove the declaration from the class (I've commented it out) and I call the function as a non class member, then it executes well... I need to be able to call that function while that function would be a part of CMyClass. Is this possible? and if it is possible, what do I'm doing wrong? Thank you in advance. NOTE: Somebody has told me that this is a weakness of the VC++ compiler, but I continue searching because I need it, what do you believe?

      J Offline
      J Offline
      jarl
      wrote on last edited by
      #2

      Firstly, the declaration you have there is not correct. ( or is it just formatting which removes the < typename T > bit ? ) Anyways, ( assuming it is not ), to declare a template member function you have to do this; in header: template< typename T > void MyFunction( T& ); ... template< typename T > void MyClass::MyFunction( T & param ) { ... } The point is that you can't define a templated method outside of the header-file where it is declared. ( The compiler might not complain, even 7 doesn't, but the linker looses it... ) Secondly, this will only partly work if you have anything earlier than VC 7. VC 6 will do it, however it might fail silently on certain cases which can cause you grief later. ( In particular if you try to specialise the member templates ) -=jarl=-

      J J 2 Replies Last reply
      0
      • J jarl

        Firstly, the declaration you have there is not correct. ( or is it just formatting which removes the < typename T > bit ? ) Anyways, ( assuming it is not ), to declare a template member function you have to do this; in header: template< typename T > void MyFunction( T& ); ... template< typename T > void MyClass::MyFunction( T & param ) { ... } The point is that you can't define a templated method outside of the header-file where it is declared. ( The compiler might not complain, even 7 doesn't, but the linker looses it... ) Secondly, this will only partly work if you have anything earlier than VC 7. VC 6 will do it, however it might fail silently on certain cases which can cause you grief later. ( In particular if you try to specialise the member templates ) -=jarl=-

        J Offline
        J Offline
        Joan M
        wrote on last edited by
        #3

        Sorry, I've corrected the syntax... could you take other look at it?

        1 Reply Last reply
        0
        • J jarl

          Firstly, the declaration you have there is not correct. ( or is it just formatting which removes the < typename T > bit ? ) Anyways, ( assuming it is not ), to declare a template member function you have to do this; in header: template< typename T > void MyFunction( T& ); ... template< typename T > void MyClass::MyFunction( T & param ) { ... } The point is that you can't define a templated method outside of the header-file where it is declared. ( The compiler might not complain, even 7 doesn't, but the linker looses it... ) Secondly, this will only partly work if you have anything earlier than VC 7. VC 6 will do it, however it might fail silently on certain cases which can cause you grief later. ( In particular if you try to specialise the member templates ) -=jarl=-

          J Offline
          J Offline
          jarl
          wrote on last edited by
          #4

          ...right, so reading, my reply again I realise that it might not be entirely clear....;P What I was trying to say was; Member function templates are poorly supported by VC 6 Member function templates are supported by VC 7, however... Member function template specialisation is not, ( not properly, anyways ), What you are trying to do, ( splitting template declaration and definition into separate files ), is the subject of the mysterious export keyword, which for numerous reasons has been deemed a red herring. Unfortunately (?), the only solution is to keep the declaration and definition in the same header file. If you want to make sure that the linker has a single instance of a template somewhere, ( and not rely on client code usage patterns ), you can instantiate it using the template keyword on its own, like so; in some cpp files somwhere... template MyTemplatedClass; this will generate all the code for MyTemplatedClas in the cpp file for the linker to use. HOWEVER....this doesn't work for template member functions, only for the whole class, ( or perhaps for free functions. ) So...'tis a bit messy... -=jarl=-

          J 1 Reply Last reply
          0
          • J jarl

            ...right, so reading, my reply again I realise that it might not be entirely clear....;P What I was trying to say was; Member function templates are poorly supported by VC 6 Member function templates are supported by VC 7, however... Member function template specialisation is not, ( not properly, anyways ), What you are trying to do, ( splitting template declaration and definition into separate files ), is the subject of the mysterious export keyword, which for numerous reasons has been deemed a red herring. Unfortunately (?), the only solution is to keep the declaration and definition in the same header file. If you want to make sure that the linker has a single instance of a template somewhere, ( and not rely on client code usage patterns ), you can instantiate it using the template keyword on its own, like so; in some cpp files somwhere... template MyTemplatedClass; this will generate all the code for MyTemplatedClas in the cpp file for the linker to use. HOWEVER....this doesn't work for template member functions, only for the whole class, ( or perhaps for free functions. ) So...'tis a bit messy... -=jarl=-

            J Offline
            J Offline
            jarl
            wrote on last edited by
            #5

            aaaarghhh....i hate this... the < typename T > bit keeps getting lost in the wash.... oh well, we now know what we're both trying to say. :) -=jarl=-

            1 Reply Last reply
            0
            • J Joan M

              Hello, I need to use a template function, but I need that that template function be a class member... Let's say that I've tried to do:

              INSIDE THE HEADER FILE:
              template<class T> void MyClass::MyFunction(T& Parameter);

              INSIDE THE IMPLEMENTATION FILE:
              template<class T> void CMyClass::MyFunction(T& Parameter)
              {
              SomeOperations...
              }

              HOW DO I CALL THE FUNCTION:
              bool b = false;
              MyFunction(b);

              The fact is that using this code I get a lot of errors (one in each MyFunction call) like: ...MyFile.cpp(119) : error C2893: Failed to specialize function template 'void __thiscall CMyclass::MyFunction(T &)' With the following template arguments: 'bool' The strange thing is that if I remove the declaration from the class (I've commented it out) and I call the function as a non class member, then it executes well... I need to be able to call that function while that function would be a part of CMyClass. Is this possible? and if it is possible, what do I'm doing wrong? Thank you in advance. NOTE: Somebody has told me that this is a weakness of the VC++ compiler, but I continue searching because I need it, what do you believe?

              J Offline
              J Offline
              Joaquin M Lopez Munoz
              wrote on last edited by
              #6

              Try this approach:

              // header

              class CMyClass
              {
              public:
              template void MyFunction(T& Parameter)
              {
              // implementation
              }
              };

              Although you can legally move the definition of MyFunction outside the body of CMyClass declaration, VC++ 6.0 crashes at it, so this is your only option (don't know about VC++ 7.0). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

              J 1 Reply Last reply
              0
              • J Joaquin M Lopez Munoz

                Try this approach:

                // header

                class CMyClass
                {
                public:
                template void MyFunction(T& Parameter)
                {
                // implementation
                }
                };

                Although you can legally move the definition of MyFunction outside the body of CMyClass declaration, VC++ 6.0 crashes at it, so this is your only option (don't know about VC++ 7.0). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                J Offline
                J Offline
                Joan M
                wrote on last edited by
                #7

                Right, this has worked well. Thank you very much.

                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