Difference between template function specialization and function overloading.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • flopbucket

    Difference between template function specialization and function overloading.

    Could someone explain to me what the difference is between function
    template specialization and function overloading?

    I guess overloading can change the number of parameters, but otherwise
    they seem very similar to me - i.e. they both provide specialized
    functions for depending on the parameter types.

    Thanks

  • Ron Natalie

    #2
    Re: Difference between template function specialization and functionoverloa ding.

    flopbucket wrote:
    Could someone explain to me what the difference is between function
    template specialization and function overloading?
    >
    I guess overloading can change the number of parameters, but otherwise
    they seem very similar to me - i.e. they both provide specialized
    functions for depending on the parameter types.
    >
    Thanks
    >
    Well, overloading has nothing to do with templates. Overloading
    means defining more than one function signature for the same name.
    There's no need for any constraint on the arguments:

    void foo(int);
    void foo();
    void foo(std::string , double);

    are all different and valid overloads. Other than the overload
    resolution, they are all unique entities...they can have different
    access classes, can be virtual or not on each function, etc..

    A template provides a generic implementation for a variety of
    types, but they all (save for defaulted args) the same number
    of arguments. A specialization is just that, a user provided
    implementation of the template instantiation rather than allowing
    the compiler to generate one from the generic template.

    Templated functions can not be virtual and they all have the
    same access.

    Comment

    • Kirit Sælensminde

      #3
      Re: Difference between template function specialization and function overloading.


      flopbucket wrote:
      Could someone explain to me what the difference is between function
      template specialization and function overloading?
      >
      I guess overloading can change the number of parameters, but otherwise
      they seem very similar to me - i.e. they both provide specialized
      functions for depending on the parameter types.
      They do different things in different ways. Overloading allows you to
      call a different function depending on the arguments given:

      void foo( int );
      void foo( double );
      void foo( int, int );

      Are all fine, but you can't also have:

      int foo( int );

      Template specialisation allows you to define a function that works for
      any type and specialising it allows you to specify a different
      implementation for some of them. Here is a good example:

      template<typena me Tinline
      std::wstring toString( const T & t ) {
      std::wstringstr eam ss;
      ss << t;
      return ss.str();
      }

      This allows you to turn most types into a string automatically, but is
      silly for strings so we do this:

      template<inline
      std::wstring toString< std::wstring >( const std::wstring &t ) {
      return t;
      }

      What we cannot do though is this:

      template<inline
      std::wstring toString< double >( double v, int places ) {
      //...
      }

      We can also have a difference in return type so we can do this:

      template< typename T >
      T variant_cast( const VARIANT &v );

      And then specialise this for those types that need to pull data out of
      the variant in odd ways.

      Hope this helps.


      K

      Comment

      • flopbucket

        #4
        Re: Difference between template function specialization and function overloading.

        >
        They do different things in different ways. Overloading allows you to
        call a different function depending on the arguments given:
        >
        void foo( int );
        void foo( double );
        void foo( int, int );
        >
        Are all fine, but you can't also have:
        >
        int foo( int );
        Right, this I know and understand. But what about the following:

        template<class T>
        void foo(const T& t) { ... }

        if I have (or can I have?) also:

        void foo(int t) { ...}

        is that then an unrelated function ? I believe it is not a
        specialization because it does not have template<in front of it. In
        that case, what if I also have:

        template<>
        void foo(int t) { ... }

        How does that interact with plain void foo(int) ?

        Thanks for all the assistance.

        Comment

        • Victor Bazarov

          #5
          Re: Difference between template function specialization and function overloading.

          flopbucket wrote:
          >They do different things in different ways. Overloading allows you to
          >call a different function depending on the arguments given:
          >>
          >void foo( int );
          >void foo( double );
          >void foo( int, int );
          >>
          >Are all fine, but you can't also have:
          >>
          >int foo( int );
          >
          Right, this I know and understand. But what about the following:
          >
          template<class T>
          void foo(const T& t) { ... }
          >
          if I have (or can I have?) also:
          >
          void foo(int t) { ...}
          >
          is that then an unrelated function ? I believe it is not a
          specialization because it does not have template<in front of it. In
          that case, what if I also have:
          >
          template<>
          void foo(int t) { ... }
          >
          How does that interact with plain void foo(int) ?
          It doesn't. It won't compile because

          template<void foo(int);

          is NOT a specialisation of the template

          template<class Tvoid foo(T const&);

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask


          Comment

          • flopbucket

            #6
            Re: Difference between template function specialization and function overloading.

            Hi,

            >
            It doesn't. It won't compile because
            >
            template<void foo(int);
            >
            is NOT a specialisation of the template
            >
            template<class Tvoid foo(T const&);
            >
            Ok, but the reason for that is the paremeter type? i.e.

            template<void foo(const int&) { ... }

            is then a specialization?

            Thanks for the information.

            Comment

            • Victor Bazarov

              #7
              Re: Difference between template function specialization and function overloading.

              flopbucket wrote:
              >It doesn't. It won't compile because
              >>
              > template<void foo(int);
              >>
              >is NOT a specialisation of the template
              >>
              > template<class Tvoid foo(T const&);
              >>
              >
              Ok, but the reason for that is the paremeter type? i.e.
              >
              template<void foo(const int&) { ... }
              >
              is then a specialization?
              Precisely!

              V
              --
              Please remove capital 'A's when replying by e-mail
              I do not respond to top-posted replies, please don't ask


              Comment

              Working...