By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

template spec issues

P: n/a
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.
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Amit wrote:
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<int>::val = 0;
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
Jul 23 '05 #2

P: n/a

Victor Bazarov wrote:
Amit wrote:
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.


Thanks for confirming.
> 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?


Maybe that's the part I overlooked.
> 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<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)
{
}
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


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.

Jul 23 '05 #3

P: n/a

Victor Bazarov wrote:
Amit wrote:
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.


Thanks for confirming.
> 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?


Maybe that's the part I overlooked.
> 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<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)
{
}
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


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.

Jul 23 '05 #4

P: n/a
am******@gmail.com wrote:
Victor Bazarov wrote:
Amit wrote:
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

Jul 23 '05 #5

P: n/a
>>I don't understand. We were talking about _explicitly_specialised_
static
data members defined outside the template specialisation. If you want todefine the member template outside the template specialisation, you willneed to follow proper syntax: template<typename T1> void Temp<int>::foo(T1 t)


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.

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.