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 

Specialized class template out-of-line members: template<>?

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





PostPosted: Tue Sep 16, 2003 9:04 pm    Post subject: Specialized class template out-of-line members: template<>? Reply with quote



Hi

Consider:

template<typename T>
struct TypeDesc
{
void addSelf() { }
};

template<>
struct TypeDesc<int>
{
void addSelf();
};

template<> void TypeDesc<int>::addSelf()
{
}

GCC complained about the template<> before the definition of addSelf(), so I
took it to Comeau which said:

"ComeauTest.c", line 12: error: "void TypeDesc<int>::addSelf()" is not an
entity
that can be explicitly specialized
template<> void TypeDesc<int>::addSelf()
^

So I hauled out CD2, which says:

14.5.1.1:
1 A member function of a class template is implicitly a member function
template with the template-parameters of its class template as its
template-parameters.

So the rules for specializing a function template should apply, right? The
"template<>" in the offending line should refer to the member function, not
the class, right?

Can somebody straighten this out for me?

Regards
David Turner



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
David B. Held
Guest





PostPosted: Wed Sep 17, 2003 7:57 pm    Post subject: Re: Specialized class template out-of-line members: template Reply with quote



"David Turner" <david (AT) firepro (DOT) co.za> wrote

Quote:
Consider:

template<typename T
struct TypeDesc
{
void addSelf() { }
};

template
struct TypeDesc {
void addSelf();
};

template<> void TypeDesc<int>::addSelf()
{
}
[...]

Since TypeDesc<int> is a concrete type, there is no need to
specify template<> for the out-of-line definition of addSelf().

Dave



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

Back to top
Greg Comeau
Guest





PostPosted: Wed Sep 17, 2003 8:18 pm    Post subject: Re: Specialized class template out-of-line members: template Reply with quote



In article <bk6s66$adk$1 (AT) ctb-nnrp2 (DOT) saix.net>,
David Turner <david (AT) firepro (DOT) co.za> wrote:
Quote:
Consider:

template<typename T
struct TypeDesc
{
void addSelf() { }
};

template
struct TypeDesc {
void addSelf();
};

template<> void TypeDesc<int>::addSelf()
{
}

GCC complained about the template<> before the definition of addSelf(), so I
took it to Comeau which said:

"ComeauTest.c", line 12: error: "void TypeDesc<int>::addSelf()" is not an
entity
that can be explicitly specialized
template<> void TypeDesc<int>::addSelf()
^

Right, in the example given above, it's a no-go.

Quote:
So I hauled out CD2, which says:

14.5.1.1:
1 A member function of a class template is implicitly a member function
template with the template-parameters of its class template as its
template-parameters.

So the rules for specializing a function template should apply, right? The
"template<>" in the offending line should refer to the member function, not
the class, right?

Can somebody straighten this out for me?

What you quote seems correct but only part of the issue, since it
does not consider the context you have above, in particular,
the explicit "full" specialization of class TypeDesc<int>. Since
you have that, then all you need is to provide a definition,
not another explicit specialization, of TypeDesc<int>::addSelf().

Consider that the template <> when defining TyepeDesc<int>
overrides the generic description of TypeDesc<T> and so
addSelf does not need to use it, and shouldn't, since at
that point, the generic definition of TypeDesc<T> won't be used.
So, if it's not clear, you just need:

void TypeDesc<int>::addSelf()
{
}

At that point, you don't need to substitute for the generic
definition but for the actual definition. You will find
compiler error messages saying you need the template<>
to explicit specialize in some contexts, but then saying
that you can't use them in other contexts, and so you need
to keep in mind the above. In fact, if the original was in
two seperate source files not together, it's possible your
program can have a problem (error). So one does need to
consider what they are specializing, and how, beforehand.
--
Greg Comeau/4.3.3:Full C++03 core language + more Windows backends
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

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

Back to top
Attila Feher
Guest





PostPosted: Fri Sep 19, 2003 10:16 am    Post subject: Re: Specialized class template out-of-line members: template Reply with quote

Greg Comeau wrote:
[SNIP]
Quote:
Can somebody straighten this out for me?

What you quote seems correct but only part of the issue, since it
does not consider the context you have above, in particular,
the explicit "full" specialization of class TypeDesc<int>. Since
you have that, then all you need is to provide a definition,
not another explicit specialization, of TypeDesc<int>::addSelf().

Consider that the template <> when defining TyepeDesc<int
overrides the generic description of TypeDesc addSelf does not need to use it, and shouldn't, since at
that point, the generic definition of TypeDesc<T> won't be used.
So, if it's not clear, you just need:

void TypeDesc<int>::addSelf()
{
}

Khm. Greg. Could you reword it in lamer language? It think I could learn
something from it, but re-reading it up to 1:30am last night did not help
and re-reading it now does not help either. I just do not understand what
you are saying. :-(

--
Attila aka WW



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