template spec issues

A

Amit

Greetings all,

I am writing some code somehwat similar to the test code I have below. I am
having a variety of issues with template specialization. I am not sure if
this is related to something i havent correctly understood
related to template specialization or is it some problem related to the
compiler.

Following is the code..

#include <iostream>
using namespace std;
class ABC
{
int i;
public :
ABC(int i = 10) { }
~ ABC() { }
void out() { cout << " the value of i is :" << i << endl;
}
};

template<typename T>
class Temp
{
static T val;
public:
Temp() { }
~Temp() { }
void output() { cout << " Primary Template " << endl; }
};

template<typename T>
T Temp<T>::val = 0;
template<>
class Temp<int>
{
static int val;
public:
Temp() { }
~Temp() { }
void output() { cout << " Spec Template for int" << val << endl;
}
};

/* //problem no 1
template<>
int Temp<int>::val = 0;
*/

/* // problem no 2
template<>
class Temp<ABC>
{
static int val;
public:
Temp() { }
~Temp() { }
void output() { cout << " Spec Template for ABC " << endl;
}
};
template<>
int Temp<ABC>::val = 0;
*/

int main(){
Temp<int> t;
t.output();
return 0;
}

1>First of all, is the code as it is, supposed to compile ? I am guessing
not, as I havent done the defintion for the static member in the first
specialization( the code after the comments..problem no 1). Interestingly,
the code complies.

2>when I introduce the code at " problem no 1", in the program and complie
it, I get an error saying "int Temp<int>::val" cannot be a template
definition. Is the way I am defining it wrong ?
3>However, when I remove the "template<> " statement at "problem no 1', and
keep the specialization as it is, the code complies, which I think is pretty
weird too ?
4>at problem no2, with the specialization for ABC, I get same results.

Thanks.
 
V

Victor Bazarov

Amit said:
Greetings all,

I am writing some code somehwat similar to the test code I have below. I am
having a variety of issues with template specialization. I am not sure if
this is related to something i havent correctly understood
related to template specialization or is it some problem related to the
compiler.

Following is the code..

#include <iostream>
using namespace std;
class ABC
{
int i;
public :
ABC(int i = 10) { }

Did you mean to do

ABC(int i = 10) : i(i) {}

???
~ ABC() { }
void out() { cout << " the value of i is :" << i << endl;
}
};

template<typename T>
class Temp
{
static T val;
public:
Temp() { }
~Temp() { }
void output() { cout << " Primary Template " << endl; }
};

template<typename T>
T Temp<T>::val = 0;
template<>
class Temp<int>
{
static int val;
public:
Temp() { }
~Temp() { }
void output() { cout << " Spec Template for int" << val << endl;
}
};

/* //problem no 1
template<>
int Temp<int>::val = 0;
*/

/* // problem no 2
template<>
class Temp<ABC>
{
static int val;
public:
Temp() { }
~Temp() { }
void output() { cout << " Spec Template for ABC " << endl;
}
};
template<>
int Temp<ABC>::val = 0;
*/

int main(){
Temp<int> t;
t.output();
return 0;
}

1>First of all, is the code as it is, supposed to compile ?

It seems well-formed. Comeau accepts it.
I am guessing
not, as I havent done the defintion for the static member in the first
specialization( the code after the comments..problem no 1).

So? Do you ever use it?
Interestingly,
the code complies.

2>when I introduce the code at " problem no 1", in the program and complie
it, I get an error saying "int Temp<int>::val" cannot be a template
definition. Is the way I am defining it wrong ?

Yes. Since you explicitly specialise the class template itself just
before, you shouldn't use the 'template<>' to define the 'val' member.
It should be

int Temp said:
3>However, when I remove the "template<> " statement at "problem no 1', and
keep the specialization as it is, the code complies, which I think is pretty
weird too ?

Why do you think that?
4>at problem no2, with the specialization for ABC, I get same results.

The same requirement of the Standard (stated in 14.7.3/5) applies.

V
 
A

amparikh

Victor said:
Did you mean to do

ABC(int i = 10) : i(i) {}

???


It seems well-formed. Comeau accepts it.

Thanks for confirming.
So? Do you ever use it?

Maybe that's the part I overlooked.
Yes. Since you explicitly specialise the class template itself just
before, you shouldn't use the 'template<>' to define the 'val' member.
It should be

int Temp<int>::val = 0;

So does the same apply with memeber templates ? In short,
if you have a member template with the class Temp as above

template <typename T1>
void f ( T1 t);

all I need for the specialized function would be
template<typename T1>
void f<int>( t1 t)
{
}
Why do you think that?
results.

The same requirement of the Standard (stated in 14.7.3/5) applies.

V

On a side note, This reply of yours, I was able to access it through
google groups. However wasnt able to see it as a reply to my original
question, through the regular newsreader I use. Would you know any
particular reason why this would happen ?

Thanks.
 
A

amparikh

Victor said:
Did you mean to do

ABC(int i = 10) : i(i) {}

???


It seems well-formed. Comeau accepts it.

Thanks for confirming.
So? Do you ever use it?

Maybe that's the part I overlooked.
Yes. Since you explicitly specialise the class template itself just
before, you shouldn't use the 'template<>' to define the 'val' member.
It should be

int Temp<int>::val = 0;

So does the same apply with memeber templates ? In short,
if you have a member template with the class Temp as above

template <typename T1>
void f ( T1 t);

all I need for the specialized function would be
template<typename T1>
void f<int>( t1 t)
{
}
Why do you think that?
results.

The same requirement of the Standard (stated in 14.7.3/5) applies.

V

On a side note, This reply of yours, I was able to access it through
google groups. However wasnt able to see it as a reply to my original
question, through the regular newsreader I use. Would you know any
particular reason why this would happen ?

Thanks.
 
V

Victor Bazarov

Victor said:
Amit said:
Greetings all,
[...]

2>when I introduce the code at " problem no 1", in the program and
complie
it, I get an error saying "int Temp<int>::val" cannot be a template
definition. Is the way I am defining it wrong ?

Yes. Since you explicitly specialise the class template itself just
before, you shouldn't use the 'template<>' to define the 'val'
member.

It should be

int Temp<int>::val = 0;


So does the same apply with memeber templates ? In short,
if you have a member template with the class Temp as above

template <typename T1>
void f ( T1 t);

all I need for the specialized function would be
template<typename T1>
void f<int>( t1 t)
{
}

I don't understand. We were talking about _explicitly_specialised_ static
data members defined outside the template specialisation. If you want to
define the member template outside the template specialisation, you will
need to follow proper syntax:

template<typename T1> void Temp<int>::foo(T1 t)

..
[...]
On a side note, This reply of yours, I was able to access it through
google groups. However wasnt able to see it as a reply to my original
question, through the regular newsreader I use. Would you know any
particular reason why this would happen ?

Your news server is too slow, probably. BTW, you replied twice...

V
 
A

amparikh

I don't understand. We were talking about _explicitly_specialised_
static
Thanks.
Sorry, It was just a typo on the syntax of the function on my part, as
I typed something in a hurry. I asked about member templates after the
question about static members, just to make sure, I understand it
correctly.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top