C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template Partial Specialization syntax.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
shri314@gmail.com
Guest





PostPosted: Sat Oct 08, 2005 5:04 pm    Post subject: Template Partial Specialization syntax. Reply with quote



Hi,

I am confused about template partial specialization syntax.


For example:
=============

// The base template.
template <class T>
struct A
{
T x;

void select()
{
std::cout << "Template T selected!!" << std::endl;
}
};


// The partial specialization.

//template <> // <---- Adding or removing this statement
// does not seem to make any apparent
// difference.
template struct A<T*>
{
T* x;

void select()
{
std::cout << "Template1 T* selected!!" << std::endl;
}
};


So is it different when "template<>" is added to partial specialization
syntax?

Thanks in advance.

Regards,
-Shriram


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Sun Oct 09, 2005 2:07 pm    Post subject: Re: Template Partial Specialization syntax. Reply with quote



[email]shri314 (AT) gmail (DOT) com[/email] wrote:
Quote:
I am confused about template partial specialization syntax.


For example:
=============

// The base template.
template <class T
struct A
{
T x;

void select()
{
std::cout << "Template T selected!!" << std::endl;
}
};


// The partial specialization.

//template <> // <---- Adding or removing this statement
// does not seem to make any apparent
// difference.

It seems to be a quirk in the compiler you're using. Normal compiler
should reject two 'template' keywords in this statement.

Quote:
template struct A {
T* x;

void select()
{
std::cout << "Template1 T* selected!!" << std::endl;
}
};


So is it different when "template<>" is added to partial
specialization syntax?

It is a syntax error.

V



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Valentin Samko
Guest





PostPosted: Sun Oct 09, 2005 8:27 pm    Post subject: Re: Template Partial Specialization syntax. Reply with quote



[email]shri314 (AT) gmail (DOT) com[/email] wrote:
Quote:
I am confused about template partial specialization syntax.
//template <> // <---- Adding or removing this statement
// does not seem to make any apparent
// difference.
template struct A

You do not need that extra template<> which is only used for the full
specialisation.

Your code should not even compile if you uncomment this line. I just
tried it with VC7.1, Comeau and g++ 3.4.2, and only g++ compiled this
code with template<> uncommented. This is probably a g++ bug.

--

Valentin Samko - http://val.samko.info

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Greg Herlihy
Guest





PostPosted: Mon Oct 10, 2005 2:44 am    Post subject: Re: Template Partial Specialization syntax. Reply with quote

[email]shri314 (AT) gmail (DOT) com[/email] wrote:
Quote:
Hi,

I am confused about template partial specialization syntax.


For example:
=============

// The base template.
template <class T
struct A
{
T x;

void select()
{
std::cout << "Template T selected!!" << std::endl;
}
};


// The partial specialization.

//template <> // <---- Adding or removing this statement
// does not seem to make any apparent
// difference.
template struct A {
T* x;

void select()
{
std::cout << "Template1 T* selected!!" << std::endl;
}
};


So is it different when "template<>" is added to partial specialization
syntax?

Thanks in advance.

Regards,
-Shriram

You have found what is almost certainly a bug in gcc (I'm assuming the
compiler used was gcc since I found that gcc 4.01 will compile the
program with the extraneous "template<>" commented in). I also found
that both CodeWarrior 9.6 and Comeau's online C++ compiler fail to
compile the same program because there is no template that encloses
class template A. If A were a member template of another class
template, for instance, than each template "layer" could be
individually declared with a template keyword. So this syntax could be
legal in some cases. But since no such complex structure is present in
this simple declaration it is not legal here.

Note that only those type parameters that have not been fully
specialized in a template specialization declaration appear in the
template's typelist. Therefore a template declaration with an empty
parameter list <> is not a partial specialization, but a full
specialization. But since the template <> should not be present in this
program, there is no full specialization for class template A, just a
partial specialization for pointer types. And though it may not be
immediately apparent, the only actual difference found in the
specialized template class A is the output string in select(). In all
other respects the specialization of A for pointer types is identical
to its primary template for any type.

Greg


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
shri314@gmail.com
Guest





PostPosted: Mon Oct 10, 2005 10:18 am    Post subject: Re: Template Partial Specialization syntax. Reply with quote

I am using gcc 4.0.1. And compiles it without problems.

Perhaphs there is some other explaination?

Regards,
-Shriram.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
chrismberry@gmail.com
Guest





PostPosted: Mon Oct 10, 2005 10:22 am    Post subject: Re: Template Partial Specialization syntax. Reply with quote

i'm just replying in passing, i've never been on these google things...
Well it looks to me like you just copied this from your code. so
perhaps you are declaring something globally... (or it should not run
at all). You define struct in two ways... one with passing a perameter,
int or not.. and the other passing a reference(pointing to that other
perameter).

just reading this code the way it stands i infer that it shouldnt be
there.
-chris


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Mon Oct 10, 2005 10:22 am    Post subject: Re: Template Partial Specialization syntax. Reply with quote

[email]shri314 (AT) gmail (DOT) com[/email] wrote:
Quote:

//template <> // <---- Adding or removing this statement
// does not seem to make any apparent
// difference.
template struct A {
T* x;

void select()
{
std::cout << "Template1 T* selected!!" << std::endl;
}
};

So is it different when "template<>" is added to partial specialization
syntax?

Which compiler are you using? If you put "template<>" it is a syntax
error. If the compiler doesn't give you an error, it is definitely
buggy. "template<>" is used for full specialization only and you can't
both partially specially *and* fully specialize. That is, either you write:

template<class T>
struct A<T*> // partial specialization
{ /* ... */ };

or

template<>
struct A<int> // full specialization (no template parameters here!)
{ /* ... */ };

but you can't put both "template<>" and "template<class T>".

HTH,

Ganesh

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Richard Corden
Guest





PostPosted: Wed Oct 12, 2005 12:54 am    Post subject: Re: Template Partial Specialization syntax. Reply with quote

[email]shri314 (AT) gmail (DOT) com[/email] wrote:
Quote:
I am using gcc 4.0.1. And compiles it without problems.

This code is probably accepted by a few compilers - but they shouldn't.
Quote:

Perhaphs there is some other explaination?

There is an explanation why G++ doesn't catch it.

A template parameter can be modelled by a depth and an index. This
allows arbitrary choice of parameter name, as well as allowing the
compiler to distinguish between member template parameters and outer
template parameters.

Anyway, when you declare an explicit specialisation such as:

template <>
struct A<int>
{
template <typename T>
struct B {};
};

There are no outer template parameters, and so the template parameters
for 'B' can start as if they are at the outer most depth. Therefore,
the 'template<>' is almost like a no-op.


For versions of G++ before 4.0.2 you could actually have:

template <typename T>
struct S
{
};

template <>
template <>
template <>
template <>
template <typename T>
struct S<T*>
{
};




Regards,

Richard


--
Richard Corden

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Bob Bell
Guest





PostPosted: Wed Oct 12, 2005 3:13 pm    Post subject: Re: Template Partial Specialization syntax. Reply with quote

[email]shri314 (AT) gmail (DOT) com[/email] wrote:
Quote:
I am using gcc 4.0.1. And compiles it without problems.

Perhaphs there is some other explaination?

Nope, no other explanation; Victor's correct.

template<class T>
struct A { };

template<>
template<class T>
struct A<T*> { };

is ill-formed.

Bob


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
shri314@gmail.com
Guest





PostPosted: Thu Oct 13, 2005 1:44 pm    Post subject: Re: Template Partial Specialization syntax. Reply with quote

Thanks a lot for all your replies.

I have filed a bug in the gcc bugzilla to track this.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24314

Regards,
-Shriram


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.