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. 5 1692
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
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.
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. 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
>>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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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)...
|
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...
|
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,...
|
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...
|
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
{
|
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...
|
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:
|
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...
|
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),...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |