Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Compilation error in template explicit specialization

Reply
Thread Tools

Compilation error in template explicit specialization

 
 
somenath
Guest
Posts: n/a
 
      11-20-2012
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[i] == 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 ?
 
Reply With Quote
 
 
 
 
Zhihao Yuan
Guest
Posts: n/a
 
      11-20-2012
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. */ }
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explicit specialization of member class template Barry C++ 2 09-30-2007 04:42 AM
illegal explicit template specialization stain C++ 3 05-30-2006 08:08 AM
What's the difference betwwen explicit instantiaion and explicit specialization? Andy C++ 5 01-30-2005 11:46 PM
Explicit and partial template specialization nested in classes. Patrick Kowalzick C++ 0 10-29-2004 02:45 PM
Is explicit template qualification required for explicit delete? J.T. Conklin C++ 1 08-11-2004 02:06 AM



Advertisments