Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem on template specialization

shuisheng
Guest
 
Posts: n/a
#1: Sep 24 '06
Dear All,

I have a simple code, but it has some problem in cimpiling. Would you
please help me to look at it?

template<class Type, size_t N>
class Vector
{
private:
Type data[N];

public:

template<size_t i>
Type Product() const
{
return data[i] * Product(i+1);
}

template<>
Type Product<N>() const
{
return 1;
}
}

The compilor error is that:

Error 1 error C2975: 'i' : invalid template argument for
'Vector<Type,N>::Product', expected compile-time constant expression

What I assume is that N is a compile-time constant expression. I
appreciate your help.

Bests,

Shuisheng


Rolf Magnus
Guest
 
Posts: n/a
#2: Sep 24 '06

re: Problem on template specialization


shuisheng wrote:
Quote:
Dear All,
>
I have a simple code, but it has some problem in cimpiling. Would you
please help me to look at it?
>
template<class Type, size_t N>
class Vector
{
private:
Type data[N];
>
public:
>
template<size_t i>
Type Product() const
{
return data[i] * Product(i+1);
There is no Product() function that takes an integer as argument in your
code.
Quote:
}
>
template<>
Type Product<N>() const
{
return 1;
}
}
>
The compilor error is that:
>
Error 1 error C2975: 'i' : invalid template argument for
'Vector<Type,N>::Product', expected compile-time constant expression
Which line does that error refer to?
Quote:
What I assume is that N is a compile-time constant expression.
Well, you haven't showed how you are instantiating your template.

shuisheng
Guest
 
Posts: n/a
#3: Sep 24 '06

re: Problem on template specialization



Rolf Magnus 写道:
Quote:
Quote:
template<class Type, size_t N>
class Vector
{
private:
Type data[N];

public:

template<size_t i>
Type Product() const
{
return data[i] * Product(i+1);
>
There is no Product() function that takes an integer as argument in your
code.
>
Quote:
}

template<>
Type Product<N>() const <<<<<<<<<<< ERROR LINE

Quote:
Quote:
{
return 1;
}
}

The compilor error is that:

Error 1 error C2975: 'i' : invalid template argument for
'Vector<Type,N>::Product', expected compile-time constant expression
>
Which line does that error refer to?
>
Quote:
What I assume is that N is a compile-time constant expression.
>
Well, you haven't showed how you are instantiating your template.
Vector<int, 4v(1);
int prod = v.Product<0>();

Thanks.

Jeff Faust
Guest
 
Posts: n/a
#4: Sep 25 '06

re: Problem on template specialization


You can't specialize a class function on the class template parameter.
You can't use 'N' as the specialization. I can dig up the standard-eze
if you'd like.

Jeff

Closed Thread