Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 24th, 2006, 07:15 PM
shuisheng
Guest
 
Posts: n/a
Default Problem on template specialization

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

  #2  
Old September 24th, 2006, 07:35 PM
Rolf Magnus
Guest
 
Posts: n/a
Default 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.

  #3  
Old September 24th, 2006, 07:55 PM
shuisheng
Guest
 
Posts: n/a
Default 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.

  #4  
Old September 25th, 2006, 02:55 AM
Jeff Faust
Guest
 
Posts: n/a
Default 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

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles