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.
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.