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 

Templates and Static function

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





PostPosted: Thu Jan 27, 2005 1:06 pm    Post subject: Templates and Static function Reply with quote



I want to build a static function inside a class using templates, such as:

class CUseful
{
public:
CUseful();
~CUseful();

static template<class T> T* functionX(int X,int Y); // Just this function is a template
// not the whole class
}

So I can use the function later as:

int main()
{
....
float *pValue = CUseful::functionX<float>(X,Y);
}

But it doesn't work (doesn't even compile). I can make CUseful a template as a class (it works
that way), but before to do that, I want to know if there's a way to do it the way I'm trying
(e.g., without declaring the entire class as template, just the function), because this way, I
don't need to declare variables each time I need to call the function (and the function itself
"qualifies" very good for static Smile)

Enviroment: MS VC++ 6.0
MS XP Pro SP2

Thanks for your help.

--
Atte.
Álvaro Palma Aste http://gorrion.die.uchile.cl/~apalma
******************************************************

"En la historia evolutiva se configura lo humano con el conversar al surgir el lenguaje
como un operar recursivo en las coordinaciones conductuales consensúales que se
da en el ámbito de un modo particular de vivir en el fluir del coemocionar de los
miembros del grupo particular de primates bípedos a que pertenecemos." [...sic]

HUMBERTO MATURANA
"Lenguaje y realidad: El origen de lo humano"


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





PostPosted: Thu Jan 27, 2005 8:49 pm    Post subject: Re: Templates and Static function Reply with quote



Álvaro Palma wrote:

Quote:
class CUseful
{
public:
CUseful();
~CUseful();

static template<class T> T* functionX(int X,int Y); // Just this function is a template
// not the whole class

That should be
template<class T> static T* functionX(int X, int Y);

Quote:
}

Falk

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

Back to top
Ulrich Eckhardt
Guest





PostPosted: Thu Jan 27, 2005 8:53 pm    Post subject: Re: Templates and Static function Reply with quote



Álvaro Palma wrote:
Quote:
class CUseful
{
public:
static template<class T> T* functionX(int X,int Y); // Just this
function is a template
// not the
whole class
}

So I can use the function later as:

int main()
{
float *pValue = CUseful::functionX<float>(X,Y);
}

But it doesn't work (doesn't even compile).

It would have been immensely useful if you had told us exactly what didn't
work, in particular the compiler messages would have been interesting.
However, the syntax is wrong, the 'static' must come after the template:

template<typename T>
static T*
function( int, int);

At least compiles with my GCC 3.

Quote:
I can make CUseful a template
as a class (it works that way), but before to do that, I want to know if
there's a way to do it the way I'm trying (e.g., without declaring the
entire class as template, just the function), because this way, I don't
need to declare variables each time I need to call the function (and the
function itself "qualifies" very good for static Smile)

Question: why do you want to put that into a class at all? Why not use a
plain function?

Quote:
Enviroment: MS VC++ 6.0

Here you have an additional problem: that compiler is far away from being a
modern compiler. One of its bugs is that (under some circumstances) it
doesn't mangle the template-parameters into function-names, only the
function-parameters are involved. In order to make above code work[1]
nonetheless, you might have to use this workaround:

template<typename T>
T*
function( int, int, T* = 0);

To make it mangle T into the function-name.

Uli

[1] you will neither get compiler nor linker errors, but runtime errors
because only one version of the function will remain in the program.

--
FAQ: http://parashift.com/c++-faq-lite/

/* bittersweet C++ */
default: break;

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


Back to top
Laurence Finston
Guest





PostPosted: Thu Jan 27, 2005 8:54 pm    Post subject: Re: Templates and Static function Reply with quote

On Thu, 27 Jan 2005, =C1lvaro Palma wrote:

Quote:
I want to build a static function inside a class using templates, such as=
:

class CUseful
{
public:
CUseful();
~CUseful();

static template<class T> T* functionX(int X,int Y);

[...]

Quote:
But it doesn't work (doesn't even compile).

This worked for me. I hope you find it helpful.

Laurence Finston
http://www.gnu.org/software/3dldf/LDF.html

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

/* ttest.c++ */

#include <iomanip>
#include <ios>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

class C
{

public:
template<class T> static void f(T);

};

template<class T>
void
C::f(T t)
{
cerr << "In `f()': `t' == " << t << endl;
return;
}


int
main (int argc, char** argv)
{


C::f(1.2);
C::f(3);

}

/* Output:
g++ ttest.c++
lfinsto1> a.out
a.out
In `f()': `t' == 1.2
In `f()': `t' == 3
lfinsto1>
*/

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

Back to top
Efrat Regev
Guest





PostPosted: Thu Jan 27, 2005 8:54 pm    Post subject: Re: Templates and Static function Reply with quote


"Álvaro Palma" <Nmena (AT) unicit (DOT) cl> wrote

Quote:
I want to build a static function inside a class using templates, such as:

class CUseful
{
public:
CUseful();
~CUseful();

static template<class T> T* functionX(int X,int Y); // Just this
function is a template
// not the
whole class
}



1. You might want to change the declaration to

template<class T>
static T * functionX(int X, int Y);

(i.e., push 'static' after 'template<class T>').

2. "C++ Templates: The Complete Guide" by Vandevoorde and Jossutis explains
these types of issues very well.



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

Back to top
Antoun Kanawati
Guest





PostPosted: Thu Jan 27, 2005 8:55 pm    Post subject: Re: Templates and Static function Reply with quote

Álvaro Palma wrote:
Quote:
I want to build a static function inside a class using templates, such as:

class CUseful
{
public:
CUseful();
~CUseful();

static template<class T> T* functionX(int X,int Y); // Just this function is a template
// not the whole class

First, you need to place the template<...> at the beginning,
not in the middle, of the template expansion; i.e.:

template<class T> static T* functionX(int X, int Y);

With this correction the code should compile; which it did under g++.

Your remaining problem is to get functionX<T> to do the desired thing.

Quote:
}

But it doesn't work (doesn't even compile). [snip]

The compiler's error message would've been nice here.

Quote:
Enviroment: MS VC++ 6.0
MS XP Pro SP2

If I recall correctly, VC++ 6 does not handle this sort of function
templates as you would expect when T is not in the argument list
of functionX. I seem to remember that the unexpected behavior can
be observed either at link-time, or run-time. Basically, the function
name does not get decorated with T, so you end up with
functionX(int,int) for all instantiations.

Disclaimer: I have not used vc6 in a very long time, so I cannot vouch
for the accuracy of my recollections.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

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

Back to top
Hyman Rosen
Guest





PostPosted: Thu Jan 27, 2005 8:56 pm    Post subject: Re: Templates and Static function Reply with quote

Álvaro Palma wrote:
Quote:
static template<class T> T* functionX(int X,int Y);

Close. It's
template<class T> static T* functionX(int X,int Y);

Quote:
float *pValue = CUseful::functionX<float>(X,Y);

Your calling syntax is fine.

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

Back to top
L.Suresh
Guest





PostPosted: Thu Jan 27, 2005 8:57 pm    Post subject: Re: Templates and Static function Reply with quote

#include <iostream>

struct A {
template<class T>
static void hi() {
std::cout << "hi" << std::endl;
}
};

int main()
{
A::template hi }


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





PostPosted: Thu Jan 27, 2005 9:27 pm    Post subject: Re: Templates and Static function Reply with quote

=C1lvaro Palma wrote:
Quote:
I want to build a static function inside a class using templates, such as=
:

class CUseful
{
static template<class T> T* functionX(int X,int Y); // Just this =
function is a template
// not the wh=
ole class
}

You has to write template<class T> static T* ...etc etc
I think this should work...

class CUseful
{
public:
=09template<class T> static T* functionX(int X,int Y);
};

--
Fabio.

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

Back to top
Maxim Yegorushkin
Guest





PostPosted: Thu Jan 27, 2005 9:27 pm    Post subject: Re: Templates and Static function Reply with quote

On 27 Jan 2005 08:06:56 -0500, =C3=81lvaro Palma <Nmena (AT) unicit (DOT) cl> wrote:

Quote:
I want to build a static function inside a class using templates, such
as:

class CUseful
{
public:
CUseful();
~CUseful();

static template<class T> T* functionX(int X,int Y);

You placed static in a wrong place, it should rather be

template<class T> static T* functionX(int X,int Y);

[...]

Quote:
Enviroment: MS VC++ 6.0
MS XP Pro SP2

MS VC++ 6.0 is rather outdated compiler and it won't compile the code
anyway. There are workarounds though...

--
Maxim Yegorushkin

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

Back to top
Hyman Rosen
Guest





PostPosted: Fri Jan 28, 2005 12:37 am    Post subject: Re: Templates and Static function Reply with quote

L.Suresh wrote:
Quote:
A::template hi<int>();

No. This use of template is only valid inside a
template, to tell the compiler that a dependent
name is a template.

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

Back to top
Álvaro Palma
Guest





PostPosted: Fri Jan 28, 2005 11:27 am    Post subject: Re: Templates and Static function Reply with quote

Falk Tannhäuser <clcppm-poster (AT) this (DOT) is.invalid> wrote:
Quote:
Álvaro Palma wrote:

class CUseful
{
public:
CUseful();
~CUseful();

static template<class T> T* functionX(int X,int Y); // Just this function is a template
// not the whole class

Thanks to all of you for your help. The problem was the f#$%@
Visual C++. After fixing the syntax to:

/* CUseful.h */
class CUseful
{
public:
CUseful();
~CUseful();
template<class T> static T* functionX(int X,int Y); // Just this function is a template
}

/* CUseful.cpp */
template<class T> T* functionX(int X,int Y)
{
T *pT;
...
return pT;
}

/* main.cpp */
float *pF = CUseful::functionX<float>(X,Y);

This last line fail in the compilation, with an error that just
says....INTERNAL COMPILER ERROR (no comments)

I'm almost sure that this is a "nice" Visual C++ feature...if
somebody can make this work in another C++ compiler, please let
me know. And thanks again to everybody that have the patience to
answer :)

--
Atte.
Álvaro Palma Aste http://gorrion.die.uchile.cl/~apalma
******************************************************

"En la historia evolutiva se configura lo humano con el conversar al surgir el lenguaje
como un operar recursivo en las coordinaciones conductuales consensúales que se
da en el ámbito de un modo particular de vivir en el fluir del coemocionar de los
miembros del grupo particular de primates bípedos a que pertenecemos." [...sic]

HUMBERTO MATURANA
"Lenguaje y realidad: El origen de lo humano"


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

Back to top
Efrat Regev
Guest





PostPosted: Sat Jan 29, 2005 5:43 am    Post subject: Re: Templates and Static function Reply with quote

"Álvaro Palma" <Nmena (AT) unicit (DOT) cl> wrote

Quote:
Falk Tannhäuser <clcppm-poster (AT) this (DOT) is.invalid> wrote:
Álvaro Palma wrote:

class CUseful
{
public:
CUseful();
~CUseful();

static template<class T> T* functionX(int X,int Y); // Just
this function is a template
// not the
whole class

Thanks to all of you for your help. The problem was the f#$%@
Visual C++. After fixing the syntax to:

/* CUseful.h */
class CUseful
{
public:
CUseful();
~CUseful();
template<class T> static T* functionX(int X,int Y); // Just this
function is a template
}

/* CUseful.cpp */
template<class T> T* functionX(int X,int Y)
{
T *pT;
...
return pT;
}

/* main.cpp */
float *pF = CUseful::functionX<float>(X,Y);



1. If you change it to the following (all in CUseful.h)

/* CUseful.h */
class CUseful
{
public:
CUseful();
~CUseful();

template<class T>
static T* functionX(int X,int Y); // Just this function is a
template
}

template<class T>
T *CUsefule::functionX(int X,int Y)
{
T *pT;
...
return pT;
}

then it works fine on VC7.1.

Your definition of functionX didn't indicate that it is a method of CUseful

2. You've split CUsefule into declarations in an .h file and definitions in
a cpp file. This is somewhat problematic for templates. You might want to
read "C++ Templates: the Complete Guide" by Vandevoorde and Josuttis on
this.

HTH



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

Back to top
Álvaro Palma
Guest





PostPosted: Mon Jan 31, 2005 8:20 pm    Post subject: Re: Templates and Static function Reply with quote

Efrat Regev <efrat_regev (AT) yahoo (DOT) com> wrote:

Quote:
1. If you change it to the following (all in CUseful.h)

/* CUseful.h */
class CUseful
{
public:
CUseful();
~CUseful();

template static T* functionX(int X,int Y); // Just this function is a
template
}

You missed a ";" here.

Quote:
template T *CUsefule::functionX(int X,int Y)
{
T *pT;
...
return pT;
}

then it works fine on VC7.1.

Unfortunatelly, it doesn't work in VC6(sp 5). The exact answer from the c=
ompiler is:

fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1794)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more info=
rmation

Quote:
Your definition of functionX didn't indicate that it is a method of CUs=
eful


Yes, thanks, I saw that. However, in the file is OK, was a typo just here=

Quote:
2. You've split CUsefule into declarations in an .h file and definition=
s in
a cpp file. This is somewhat problematic for templates. You might want =
to
read "C++ Templates: the Complete Guide" by Vandevoorde and Josuttis on
this.

Ok, thanks again for your help. I guess I'm gonna have to see another way=
s to fix this.

--
Atte.
=C1lvaro Palma Aste http://gorrion.die.uchile.cl/~apalma
******************************************************

"En la historia evolutiva se configura lo humano con el conversar al surg=
ir el lenguaje
como un operar recursivo en las coordinaciones conductuales consens=FAale=
s que se
da en el =E1mbito de un modo particular de vivir en el fluir del coemocio=
nar de los
miembros del grupo particular de primates b=EDpedos a que pertenecemos." =
[...sic]

HUMBERTO MATURANA
"Lenguaje y realidad: El origen de lo humano"


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

Back to top
Allan W
Guest





PostPosted: Tue Feb 01, 2005 9:26 am    Post subject: Re: Templates and Static function Reply with quote

Álvaro Palma wrote:
Quote:
Thanks to all of you for your help. The problem was the f#$%@
Visual C++. After fixing the syntax to:

Presuming that "f#$%@" means that you would have cursed at Microsoft,
if this wasn't a moderated group.

Later, Álvaro Palma also wrote:
Quote:
Unfortunatelly, it doesn't work in VC6(sp 5). The exact answer
from the compiler is:
fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1794)
Please choose the Technical Support command on the Visual
C++
Help menu, or open the Technical Support help file
for more information

Hmm:

VC6 is copyright 1998. C++98 first edition is copyright 1998-09-01.
Meanwhile, not sure when VC7.0 was released, but VC7.1 is copyright
2002.
I think that Microsoft's policy (someone WILL correct me if I'm wrong)
is to support a product for 2 years after a new version has replaced
it.
If so, that would bring it to 2004 at the very latest, and probably
much earlier.

FWIW, VC7.1 compiles this code without diagnostics and gives the
answers
I expect.

If you must bash Microsoft, at least do it fairly. You're condemning
a pre-standard compiler for not complying with a part of the standard.
IIRC nobody else got this right either, until early 2000 (might have
been late 1999).


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