Difference between member functions for a template class defined inside and outside of the class

后端 未结 3 1276
太阳男子
太阳男子 2021-02-14 11:43

Is there a difference between defining member functions for a template class inside the class declaration versus outside?

Defined inside:

template 

        
相关标签:
3条回答
  • 2021-02-14 12:26

    Yes, the exact same is true for template classes.

    The reason why method definitions for template classes are usually preferred to be inline is that with templates, the entire definition must be visible when the template is instantiated.

    So if you put the function definition in some separate .cpp file, you'll get a linker error. The only general solution is to make the function inline, either by defining it inside the class or outside with the inline keyword. but in either cases, it must be visible anywhere the function is called, which means it must typically be in the same header as the class definition.

    0 讨论(0)
  • 2021-02-14 12:30

    I know this..I think it must be some what help full to u?

    defining a member function outside of its template
    

    It is not ok to provide the definition of a member function of a template class like this:

     // This might be in a header file:
     template <typename T>
     class xyz {
        void foo();
      };
    

    // ...

     // This might be later on in the header file:
      void xyz<T>::foo() {
    // generic definition of foo()
       }
    

    This is wrong for a few reasons. So is this:

          void xyz<class T>::foo() {
             // generic definition of foo()
          }
    

    The proper definition needs the template keyword and the same template arguments that the class template's definition was declared with. So that gives:

           template <typename T>
              void xyz<T>::foo() {
               // generic definition of foo()
                     }
    

    Note that there are other types of template directives, such as member templates, etc., and each takes on their own form. What's important is to know which you have so you know how to write each flavor. This is so especially since the error messages of some compilers may not be clear as to what is wrong. And of course, get a good and up to date book.

    If you have a nested member template within a template:

        template <typename T>
          struct xyz {
          // ...
          template <typename U>
           struct abc;
            // ...
           };
    

    How do you define abc outside of xyz? This does not work:

         template <typename U>
        struct xyz::abc<U> { // Nope
          // ...
      };
    

    nor does this:

     template <typename T, typename U>
     struct xyz<T>::abc<U> { // Nope
    // ...
     };
    

    You would have to do this:

         template <typename T>
           template <typename U>
               struct xyz<T>::abc {
                // ...
               };
    

    Note that it's ...abc not ...abc<U> because abc is a "primary" template. IOWs, this is not good:

    // not allowed here: template template struct xyz::abc { };

    0 讨论(0)
  • 2021-02-14 12:45

    There's no difference, aside from having to type more. That includes the template bit, the inline and having to use more "elaborate" names when referring to the class. For example

    template <typename T> class A { 
      A method(A a) { 
        // whatever
      } 
    }; 
    
    template <typename T> inline A<T> A<T>::method(A a) { 
      // whatever
    } 
    

    Note that when the method is defined inside you can always omit the template parameter list <T> when referring to A<T> and just use A. When defining it outside, you have to use the "full" name in the return type and in the name of the method (but not in the parameter list).

    0 讨论(0)
提交回复
热议问题