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 

How do you typdef a function template

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





PostPosted: Mon Oct 24, 2005 12:38 pm    Post subject: How do you typdef a function template Reply with quote



I wish to typedef a templated function for readability but cannot find
the correct syntax to do so. For example:

#include <iostream>

template< int I >
void foo()
{
std::cout << I << std::endl;
}

typedef foo< 2 > foo2; // <--- I wish to do something like this

int main( int argc, char* argv[] )
{
foo< 1 >();
foo2();

return 0;
}

In the above code I want to create a typedef for the function template
foo() using the value 2 as the template parameter. Is this even
possible?

Cheers,

Mark.


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

Back to top
Ahti Legonkov
Guest





PostPosted: Mon Oct 24, 2005 4:37 pm    Post subject: Re: How do you typdef a function template Reply with quote



Mark Snelling wrote:

Quote:
template< int I
void foo() { ... }

typedef foo< 2 > foo2; // <--- I wish to do something like this

[...]

In the above code I want to create a typedef for the function template
foo() using the value 2 as the template parameter. Is this even
possible?

It's possible to achieve the effect you are looking for, but not
using typedef. Write a simple inline function instead:

inline void foo2() { foo<2>(); }

--
Ahti Legonkov
leg zero at hot dot ee


[ 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: Mon Oct 24, 2005 4:39 pm    Post subject: Re: How do you typdef a function template Reply with quote



Mark Snelling wrote:
Quote:
I wish to typedef a templated function for readability

You cannot do this, because typedefs are aliases for
types, not functions.

Quote:
template< int I > void foo() { std::cout << I << std::endl; }
typedef foo< 2 > foo2; // <--- I wish to do something like this

You can use a function reference or pointer:
void (&foo2)() = foo<2>;
void (*foo3)() = foo<3>;
int main() { foo<1>(); foo2(); foo3(); }

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


Back to top
Thomas Maeder
Guest





PostPosted: Mon Oct 24, 2005 4:41 pm    Post subject: Re: How do you typdef a function template Reply with quote

"Mark Snelling" <mark.snelling (AT) gmail (DOT) com> writes:

Quote:
I wish to typedef a templated function for readability but cannot find
the correct syntax to do so. For example:

#include <iostream

You also have to #include but not necessarily std::endl.

Quote:

template< int I
void foo()
{
std::cout << I << std::endl;
}

typedef foo< 2 > foo2; // <--- I wish to do something like this

Why a typedef? foo<2> doesn't name a type.

Does creating a function pointer help? E.g.:

void (* const foo2)() = &foo<2>;


Quote:
int main( int argc, char* argv[] )
{
foo< 1 >();
foo2();

return 0;
}

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


Back to top
Carl Barron
Guest





PostPosted: Mon Oct 24, 2005 4:43 pm    Post subject: Re: How do you typdef a function template Reply with quote

In article <1130151737.073319.10660 (AT) f14g2000cwb (DOT) googlegroups.com>, Mark
Snelling <mark.snelling (AT) gmail (DOT) com> wrote:

Quote:
I wish to typedef a templated function for readability but cannot find
the correct syntax to do so. For example:

#include <iostream

template< int I
void foo()
{
std::cout << I << std::endl;
}

typedef foo< 2 > foo2; // <--- I wish to do something like this

inline foo2() {foo<2>();}


should do the equivalent of a typedef especially if the compiler
inlines anything at all.

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


Back to top
Dan McLeran
Guest





PostPosted: Mon Oct 24, 2005 4:44 pm    Post subject: Re: How do you typdef a function template Reply with quote

If you want to do something like this, you need to declare an object of
type function pointer. You can do something like this:

#include <iostream>

template< int I >
void foo()
{
std::cout << I << std::endl;

}

//typedef foo< 2 > foo2; // <--- I wish to do something like this
typedef void (*FunctionType)();//type = function with no return value
and takes no parameters

FunctionType foo2 = &foo<2>;//foo2 = object of type FunctionType

int main( int argc, char* argv[] )
{
foo< 1 >();
foo2();

return 0;
}

Also check out boost::function and boost::bind.


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

Back to top
archerzz
Guest





PostPosted: Tue Oct 25, 2005 11:23 am    Post subject: Re: How do you typdef a function template Reply with quote

I think functor may satisfy your needs. For example:

#include <iostream>

template<int i>
struct foo
{
void operator()()
{
std::cout << i << std::endl;
}
};

foo<2> foo2;
int main(int argc, char *argv[])
{
foo2();
return 0;
}
You can defined arbitary versions of foo<int i>. Is that OK?


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

Back to top
Fhulu Lidzhade
Guest





PostPosted: Tue Oct 25, 2005 12:46 pm    Post subject: Re: How do you typdef a function template Reply with quote

How about

template <void (*fptr)()>
struct func_t
{
void operator()()
{
(*fptr)();
}
};

int main()
{

typedef funct_t<foo<1> > foo1;
typedef func_t<foo<2> > foo2;

//call syntax changes somewhat
foo1()();
foo2()();
}

This way you have wrapped a function with type information, but you are
not incurring any call overhead that may be associated with
boost::function or boost::bind. You can also use the same technique for
non-template functions.

Cheers,
Fhulu


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

Back to top
Fhulu Lidzhade
Guest





PostPosted: Tue Oct 25, 2005 4:31 pm    Post subject: Re: How do you typdef a function template Reply with quote

actually i'm wrong, you don't have to change the call syntax:

typedef funct_t<foo<1> > foo1;
typedef funct_t<foo<1> > foo2;;

foo1 f1;
f1();

foo2 f2;
f2();


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

Back to top
werasm
Guest





PostPosted: Wed Oct 26, 2005 1:09 pm    Post subject: Re: How do you typdef a function template Reply with quote


Fhulu Lidzhade wrote:
Quote:
How about

template struct func_t
{
void operator()()
{
(*fptr)();
}
};
Hi F,


Say, strange name - are you from Africa?

The down side is that for each new signature, a new template is
required. Functions can have any amount of arbitrary signatures... BTW
(off topic), what include strategy do you prefer Wink (- be bold now).

Quote:

Cheers,
Fhulu

W


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


Back to top
vb
Guest





PostPosted: Wed Oct 26, 2005 1:17 pm    Post subject: Re: How do you typdef a function template Reply with quote

The value of the topic is that the author is looking for a shorter
notation on one hand (perhaps out of practical programming needs), and
also in mix-n-match of the template/typedef keywords on the other hand
(which constitutes an unusual combination of keywords).

AFAIU, you should distinguish between TYPES and INSTANCES of those
types:

foo<2> is not a type, it's an instantiation of the type void (*)()

The below compiles under VS2003:

#include "stdafx.h"
#include <iostream>

template< int I >
void foo()
{
std::cout << I << std::endl;
}

typedef void (*TFOO)();
TFOO foo2 = foo<2>;

int _tmain(int argc, _TCHAR* argv[])
{
foo< 1 >();
foo2();

return 0;
}


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