Novice Question: Function Template Specialization

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

    Novice Question: Function Template Specialization

    While I was testing my understanding of Functioin Template features by
    playing with simple function templates, I got into a problem which I
    cannot understand. I would be very grateful if someone offered me a
    kind explanation. TIA

    Function template and specialization below works fine:

    template <typename T>
    T maximum (T a, T b)
    {
    cout << "Template Called: ";
    return a < b ? b: a;
    }

    #include <cstring>
    template<>
    const char * maximum<const char *>(const char * a, const char * b)
    {
    cout << "Normal called: ";
    return strcmp(a,b) < 0 ? b: a;
    }

    BUT if I change the template to accept const reference to T like
    below:
    template <typename T>
    const T &maximum (const T & a, const T & b)
    {
    cout << "Template Called: ";
    return a < b ? b: a;
    }

    #include <cstring>
    template<>
    const char * maximum<const char *>(const char * a, const char * b)
    {
    cout << "Normal called: ";
    return strcmp(a,b) < 0 ? b: a;
    }

    The compiler (g++ v3.2) generates the following error message.

    tt.cpp:25: template-id `maximum<const char*>' for `const char*
    maximum(const
    char*, const char*)' does not match any template declaration

    Can anyone explain what the problem is?

    P.S I am trying to learn the features by playing around. So I would
    really appreciate an explanation as to what is causing the problem
    rather than "You should overload the template with normal function",
    etc types of replies. Thank you very much in advance.
  • Rolf Magnus

    #2
    Re: Novice Question: Function Template Specialization

    CoolPint wrote:
    [color=blue]
    > While I was testing my understanding of Functioin Template features by
    > playing with simple function templates, I got into a problem which I
    > cannot understand. I would be very grateful if someone offered me a
    > kind explanation. TIA
    >
    > Function template and specialization below works fine:
    >
    > template <typename T>
    > T maximum (T a, T b)
    > {
    > cout << "Template Called: ";
    > return a < b ? b: a;
    > }
    >
    > #include <cstring>
    > template<>
    > const char * maximum<const char *>(const char * a, const char * b)
    > {
    > cout << "Normal called: ";
    > return strcmp(a,b) < 0 ? b: a;
    > }
    >
    > BUT if I change the template to accept const reference to T like
    > below:
    > template <typename T>
    > const T &maximum (const T & a, const T & b)
    > {
    > cout << "Template Called: ";
    > return a < b ? b: a;
    > }
    >
    > #include <cstring>
    > template<>
    > const char * maximum<const char *>(const char * a, const char * b)
    > {
    > cout << "Normal called: ";
    > return strcmp(a,b) < 0 ? b: a;
    > }
    >
    > The compiler (g++ v3.2) generates the following error message.
    >
    > tt.cpp:25: template-id `maximum<const char*>' for `const char*
    > maximum(const
    > char*, const char*)' does not match any template declaration
    >
    > Can anyone explain what the problem is?[/color]

    Your specialization doesn't fit the template. The template has
    references to const as parameters and return type, but the
    specialization uses a non-const non-reference type. Try:

    #include <cstring>
    template<>
    const char * const & maximum<const char *>(const char * const & a,
    const char * const & b)
    {
    cout << "Normal called: ";
    return strcmp(a,b) < 0 ? b: a;
    }

    Comment

    • tom_usenet

      #3
      Re: Novice Question: Function Template Specialization

      On 19 Nov 2003 07:44:49 -0800, coolpint@yahoo. co.uk (CoolPint) wrote:
      [color=blue]
      >While I was testing my understanding of Functioin Template features by
      >playing with simple function templates, I got into a problem which I
      >cannot understand. I would be very grateful if someone offered me a
      >kind explanation. TIA[/color]
      [snip][color=blue]
      >BUT if I change the template to accept const reference to T like
      >below:
      >template <typename T>
      >const T &maximum (const T & a, const T & b)
      >{
      > cout << "Template Called: ";
      > return a < b ? b: a;
      >}
      >
      >#include <cstring>
      >template<>
      >const char * maximum<const char *>(const char * a, const char * b)
      >{
      > cout << "Normal called: ";
      > return strcmp(a,b) < 0 ? b: a;
      >}
      >
      >The compiler (g++ v3.2) generates the following error message.
      >
      >tt.cpp:25: template-id `maximum<const char*>' for `const char*
      >maximum(cons t
      > char*, const char*)' does not match any template declaration
      >
      >Can anyone explain what the problem is?[/color]

      A specialization has to have the same arguments as the source
      template, otherwise it isn't a specialization of that template.
      Substituting T=const char* gives:

      #include <cstring>
      template<>
      inline const char*& maximum<const char*>(const char*& a, const char*&
      b)
      {
      cout << "Normal called: ";
      return strcmp(a,b) < 0 ? b: a;
      }

      If you inline it, the reference parameters shouldn't add any overhead
      (and you can define it in a header).

      Tom

      Comment

      • Gianni Mariani

        #4
        Re: Novice Question: Function Template Specialization

        CoolPint wrote:[color=blue]
        > While I was testing my understanding of Functioin Template features by
        > playing with simple function templates, I got into a problem which I
        > cannot understand. I would be very grateful if someone offered me a
        > kind explanation. TIA
        >
        > Function template and specialization below works fine:
        >
        > template <typename T>
        > T maximum (T a, T b)
        > {
        > cout << "Template Called: ";
        > return a < b ? b: a;
        > }
        >
        > #include <cstring>
        > template<>
        > const char * maximum<const char *>(const char * a, const char * b)
        > {
        > cout << "Normal called: ";
        > return strcmp(a,b) < 0 ? b: a;
        > }
        >
        > BUT if I change the template to accept const reference to T like
        > below:
        > template <typename T>
        > const T &maximum (const T & a, const T & b)[/color]

        This template is for const references !
        [color=blue]
        > {
        > cout << "Template Called: ";
        > return a < b ? b: a;
        > }
        >
        > #include <cstring>
        > template<>
        > const char * maximum<const char *>(const char * a, const char * b)[/color]

        This has no references !

        Try:

        const char * &maximum<con st char *>(const char * &a, const char * &b)
        [color=blue]
        > {
        > cout << "Normal called: ";
        > return strcmp(a,b) < 0 ? b: a;
        > }
        >
        > The compiler (g++ v3.2) generates the following error message.
        >
        > tt.cpp:25: template-id `maximum<const char*>' for `const char*
        > maximum(const
        > char*, const char*)' does not match any template declaration
        >
        > Can anyone explain what the problem is?[/color]

        See comments above.

        Comment

        • tom_usenet

          #5
          Re: Novice Question: Function Template Specialization

          On Wed, 19 Nov 2003 16:07:16 +0000, tom_usenet
          <tom_usenet@hot mail.com> wrote:
          [color=blue]
          >A specialization has to have the same arguments as the source
          >template, otherwise it isn't a specialization of that template.
          >Substituting T=const char* gives:
          >
          >#include <cstring>
          >template<>
          >inline const char*& maximum<const char*>(const char*& a, const char*&
          >b)[/color]

          Doh! As Rolf says, it doesn't give that at all, but:

          template<>
          inline const char* const& maximum<const char*>(const char* const& a,
          const char* const& b)

          Tom

          Comment

          • CoolPint

            #6
            Re: Novice Question: Function Template Specialization: Still having the problem

            > Try:[color=blue]
            >
            > const char * &maximum<con st char *>(const char * &a, const char * &b)
            >[color=green]
            > > {
            > > cout << "Normal called: ";
            > > return strcmp(a,b) < 0 ? b: a;
            > > }
            > >[/color][/color]

            Thank you for answering my question. All the replies suggested the
            same solution (to have the parameters to be const reference to make
            them same as
            the original template function).

            I think I tried doing that before posting the question. Anyhow, I
            tried again after your replies but unfortunately the compiler still
            complains!

            tt.cpp:26: template-id `maximum<const char*>' for `const char*&
            maximum(const
            char*&, const char*&)' does not match any template declaration

            Is it my compiler or am I still missing something? I would appreciate
            further answer/explanation on this problem. TIA

            Comment

            • Gianni Mariani

              #7
              Re: Novice Question: Function Template Specialization: Still havingthe problem

              CoolPint wrote:[color=blue][color=green]
              >>Try:
              >>
              >>const char * &maximum<con st char *>(const char * &a, const char * &b)
              >>
              >>[color=darkred]
              >>>{
              >>> cout << "Normal called: ";
              >>> return strcmp(a,b) < 0 ? b: a;
              >>>}
              >>>[/color][/color]
              >
              >
              > Thank you for answering my question. All the replies suggested the
              > same solution (to have the parameters to be const reference to make
              > them same as
              > the original template function).
              >
              > I think I tried doing that before posting the question. Anyhow, I
              > tried again after your replies but unfortunately the compiler still
              > complains!
              >
              > tt.cpp:26: template-id `maximum<const char*>' for `const char*&
              > maximum(const
              > char*&, const char*&)' does not match any template declaration
              >
              > Is it my compiler or am I still missing something? I would appreciate
              > further answer/explanation on this problem. TIA[/color]

              Tom's second reply got it right.

              i.e.
              template <typename T>
              const T & maximum (const T & a, const T & b);

              template<>
              const char * const & maximum<const char *>(const char * const & a, const
              char * const & b)
              {
              ....
              }

              Comment

              • CoolPint

                #8
                Re: Novice Question: Function Template Specialization: Still having the problem

                Gianni Mariani <gi2nospam@mari ani.ws> wrote in message news:<bpj8eg$ll t@dispatch.conc entric.net>...[color=blue]
                > Tom's second reply got it right.
                >
                > i.e.
                > template <typename T>
                > const T & maximum (const T & a, const T & b);
                >
                > template<>
                > const char * const & maximum<const char *>(const char * const & a, const
                > char * const & b)
                > {
                > ...
                > }[/color]

                Thank you for that. I tried various combinations and I missed his
                second reply. It works. Thank you all those who spent time to answer
                my silly question.

                Now I see that specialization has to specialize the very same format
                off "function parameter" of the ogiginal function template.

                Comment

                Working...