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 

Temlpate function members syntax

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





PostPosted: Sat Dec 13, 2003 2:07 am    Post subject: Temlpate function members syntax Reply with quote



Does anybody can help me understand the syntax for defining members
templates out of classes (please see below an example)? Thanks
Regards.
Fabio

***************************************************

The following compiles and runs fine.

template <int N>
struct X
{
template <int M>
int foo( int z ) { return M+N+z; };
};

int testTemplate( int z )
{
X<4> x;
return x.foo<5>( z);
}

However, I would like to define the body of foo outside the class
declaration. Do you know what is the syntax to do that?

I have tried many different possibilities, but the compiler (VC7)
doesn't like any of them.
For example, this doesn't work...

template <int N, int M>
inline int X<N>::foo<M>( int z )
{
return M*N*z;
};

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





PostPosted: Sun Dec 14, 2003 2:03 am    Post subject: Re: Temlpate function members syntax Reply with quote



Fabio Cannizzo wrote:
Quote:
[...] I would like to define the body of foo outside the class
declaration. Do you know what is the syntax to do that?

I have tried many different possibilities, but the compiler (VC7)
doesn't like any of them.
For example, this doesn't work...

template <int N, int M
inline int X {
return M*N*z;
};

Actually, in several old compilers, this syntax was accepted, and was
the only accepted syntax for out of line member template definitions.
However, as you say, it isn't legal. The correct syntax is

template <int N>
template <int M>
inline int X<N>::foo<M>( int z )
{
return M*N*z;
}

(Incidentally, a semicolon at the end of an function definition at
namespace scope is illegal. Intriguingly, semicolon at the end of a
function definition at class scope *is* legal albeit unnecessary.)

--
Richard Smith

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

Back to top
Fabio Cannizzo
Guest





PostPosted: Mon Dec 15, 2003 10:18 am    Post subject: Re: Temlpate function members syntax Reply with quote



Richard,

thanks for your reply.

I tried what you sugested... but it doesn't work.
I also tried adding curle brackets after each template definition, but
still doesn't work ('unrecognizable template declaration'). Do you
think it is a bug of my compiler (VC7)? Anbody else had a similar
problem?

Thanks.
Regards,
Fabio


template <int N>
struct X
{
template <int M>
int foo( int z );// { return M+N+z; };
};


template <int N>
template <int M>
int X<N>::foo<M>( int z )
{
return M*N*z;
}

int __cdecl main(int argc, char* argv[])
{
int y = X<4>().foo<5>( 10);
return y;
}



[email]richard (AT) ex-parrot (DOT) com[/email] (Richard Smith) wrote in message news:<1a0929fa.0312131640.659cab38 (AT) posting (DOT) google.com>...
Quote:
Fabio Cannizzo wrote:
[...] I would like to define the body of foo outside the class
declaration. Do you know what is the syntax to do that?

I have tried many different possibilities, but the compiler (VC7)
doesn't like any of them.
For example, this doesn't work...

template <int N, int M
inline int X {
return M*N*z;
};

Actually, in several old compilers, this syntax was accepted, and was
the only accepted syntax for out of line member template definitions.
However, as you say, it isn't legal. The correct syntax is

template <int N
template inline int X {
return M*N*z;
}

(Incidentally, a semicolon at the end of an function definition at
namespace scope is illegal. Intriguingly, semicolon at the end of a
function definition at class scope *is* legal albeit unnecessary.)

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

Back to top
Graeme Prentice
Guest





PostPosted: Tue Dec 16, 2003 3:35 pm    Post subject: Re: Temlpate function members syntax Reply with quote

On 15 Dec 2003 05:18:46 -0500, [email]cannf1 (AT) bp (DOT) com[/email] (Fabio Cannizzo) wrote:

Quote:

I tried what you sugested... but it doesn't work.
I also tried adding curle brackets after each template definition, but
still doesn't work ('unrecognizable template declaration'). Do you
think it is a bug of my compiler (VC7)? Anbody else had a similar
problem?

VC6 and VC7 don't support member templates - you have to define the
function within the class definition. VC7.1 supports member templates
and this is what the code should look like in a compliant compiler (note
no angle brackets following foo in int X<N>::foo(int z) )

template <int N>
struct X
{
template <int M>
int foo( int z );// { return M+N+z; };
};


template <int N>
template <int M>
int X<N>::foo( int z )
{
return M*N*z;
}

int main()
{
int y = X<4>().foo<5>( 10);
return y;
}


For VC7, the in-class definition would look like this (as you appear to
realise)

template <int M>
int foo( int z )
{ return M+N+z; }


The following syntax which you attempted with VC7 is incorrect

template <int N>
template <int M>
int X<N>::foo<M>( int z )
{
return M*N*z;
}

because angle brackets immediately following foo, are used to define an
explicit specialization, e.g. you can do this

template <>
template <>
int X<4>::foo<5>( int z )
{
return 4*5*z;
}

but foo<M> is illegal because it is not an explicit specialization. It
is suggestive of partial specialization, which is not possible for
template functions.

Graeme

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