472,779 Members | 2,690 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

template spec issues

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
5 1656
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

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

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
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
>>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Alexander Stippler | last post by:
Hi, what do I have to do to get this (incorrect) piece of code to work. The specialization is wrong, but how can I do it? template <typename T, typename V> class Mask { public: Mask(int i)...
1
by: Jon Slaughter | last post by:
I've managed to put together a template class that basicaly creates a recursive tree that lets you easily specify the "base" class of that tree and and ending notes and lets you stop the recursive...
1
by: Joseph Turian | last post by:
How can I specialize the value of only one template parameter? Here's the fragment of code I'd like to get working: typedef enum {START, PARENT, CHILD, END} locator_ty; template <locator_ty L,...
2
by: Imre | last post by:
Hi I'd like to know what the problem is with the following code. I've tried compiling it with two compilers (VC++ 8.0, and Comeau online compiler), and both failed to compile it, saying that...
7
by: Markus Petermann | last post by:
Hello, I have a small problem I want to demonstrate with a small demo program: -------------- Snip -------------- #include <string> namespace {
35
by: Steven T. Hatton | last post by:
Perhaps I'm just a bit frustrated, and I will soon realize the clear truth of the matter, but right now I have some serious misgivings about the value of investing a lot of time and effort into...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
6
by: year1943 | last post by:
For template <typename Tclass My ; I can define partial spec-ns somewhat like template <typename Tclass My<T*; or template <typename Tclass My<Another<T ; And full spec-n, say template <class...
1
by: jason.cipriani | last post by:
Here is an example with 3 files, containing a template structure and also a template function. The header A.h declares a template structure A with a default (i.e. for any template parameter),...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.