| View previous topic :: View next topic |
| Author |
Message |
Mark Snelling Guest
|
Posted: Mon Oct 24, 2005 12:38 pm Post subject: How do you typdef a function template |
|
|
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
|
Posted: Mon Oct 24, 2005 4:37 pm Post subject: Re: How do you typdef a function template |
|
|
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
|
Posted: Mon Oct 24, 2005 4:39 pm Post subject: Re: How do you typdef a function template |
|
|
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
|
Posted: Mon Oct 24, 2005 4:41 pm Post subject: Re: How do you typdef a function template |
|
|
"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
|
Posted: Mon Oct 24, 2005 4:43 pm Post subject: Re: How do you typdef a function template |
|
|
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
|
Posted: Mon Oct 24, 2005 4:44 pm Post subject: Re: How do you typdef a function template |
|
|
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
|
Posted: Tue Oct 25, 2005 11:23 am Post subject: Re: How do you typdef a function template |
|
|
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
|
Posted: Tue Oct 25, 2005 12:46 pm Post subject: Re: How do you typdef a function template |
|
|
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
|
Posted: Tue Oct 25, 2005 4:31 pm Post subject: Re: How do you typdef a function template |
|
|
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
|
Posted: Wed Oct 26, 2005 1:09 pm Post subject: Re: How do you typdef a function template |
|
|
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 (- be bold now).
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
|
Posted: Wed Oct 26, 2005 1:17 pm Post subject: Re: How do you typdef a function template |
|
|
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 |
|
 |
|