Compilation error in template explicit specialization

Discussion in 'C++' started by somenath, Nov 20, 2012.

  1. somenath

    somenath Guest

    Hi All,
    I am trying to define explicit specialization for the following
    template but I am getting compilation error

    #ifndef COUNT_H
    #define COUNT_H

    template <class T,int size >
    int count( const T (&array)[size],T elem)
    {
    int i;
    int count = 0;
    for ( i = 0; i<size; i++)
    {
    if ( array == elem )
    {
    count +=1;

    }

    }
    return count;
    }

    template<> int count< const char * ,int size > ( const char *
    (&array)[size ], const char *x) { }
    #endif

    File count_demo.cpp

    #include <iostream>
    #include "count.h"
    using namespace std;
    int main(void)
    {
    return 0;
    }

    g++ count_demo.cpp
    In file included from count_demo.cpp:2:0:
    count.h:21:16: error: parse error in template argument list
    count.h:21:78: error: ‘size’ was not declared in this scope
    count.h:21:84: error: expected ‘)’ before ‘,’ token
    count.h:21:16: error: template-id ‘count<const char*, <expression
    error> >’ for ‘int count(...)’ does not match any template declaration

    If I remove the special template for const char * it compiles fine but
    with the special template I am getting the following error.
    Please let me know what is the mistake I am doing ?
     
    somenath, Nov 20, 2012
    #1
    1. Advertising

  2. somenath

    Zhihao Yuan Guest

    There are two problems:

    > template <class T,int size >

    `int size' is a non-type template parameter. In an explicit
    specialization, it expected to be filled in an actual constant, like

    template<> int count<const char *, 20>

    If you need it to be also customizable, you need partial specialization.
    However, a function template does not support it, so you will finally
    need a functor.

    > template<> int count< const char * ,int size > ( const char *
    > (&array)[size ], const char *x) { }


    You replaced `T' with `const char *', than how about the `const T'
    used in main template? That's `const char * const' actually.

    The following one compiles, but as I said, you do need a functor.

    template<>
    int count<const char *, 20>(const char * const (&array)[20],
    const char * x) { /* return sth. */ }
     
    Zhihao Yuan, Nov 20, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J.T. Conklin
    Replies:
    1
    Views:
    445
    David Hilsee
    Aug 11, 2004
  2. Patrick Kowalzick
    Replies:
    0
    Views:
    820
    Patrick Kowalzick
    Oct 29, 2004
  3. Andy
    Replies:
    5
    Views:
    513
    Shezan Baig
    Jan 30, 2005
  4. stain
    Replies:
    3
    Views:
    958
    stain
    May 30, 2006
  5. Barry
    Replies:
    2
    Views:
    366
    Barry
    Sep 30, 2007
Loading...

Share This Page