By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,616 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

How do you typdef a function template

P: n/a
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.

Oct 24 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
"Mark Snelling" <ma***********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com
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?


You can't typedef regular functions, so why should you be able to typedef
template functions? A function is not a type. A function pointer is a type,
but that is a different matter.

You could try

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

--
John Carson

Oct 24 '05 #2

P: n/a
Mark Snelling wrote:
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?


No. foo<2> is a function, not a type. You may do

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

Oct 24 '05 #3

P: n/a
Thanks, I didn't realise you couldn't typedef a function.

Oct 24 '05 #4

P: n/a

Mark Snelling wrote:
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?


No, it is not possible, nor does the attempt make much sense. Why
declare a function template in order to use the number 2? The function
is free to use the value 2 (or any other constant value) whenever and
however it wants.

Greg

Oct 24 '05 #5

P: n/a
Thanks for stating that what I wanted to do is not possible. However,
saying that 'the attempt doesn't make sense' assumes knowledge about
the actual problem on which the supplied trivial was based.

Oct 24 '05 #6

P: n/a
Mark Snelling wrote:
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?


foo<2> is not a type. It is a constant of type void function of void. Thus
you need a global constant:

#include <iostream>

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

}

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

int main ( void ) {
foo2();
}
Best

Kai-Uwe Bux
Oct 24 '05 #7

P: n/a

"Mark Snelling" <ma***********@gmail.com> wrote in message
news:11********************@g14g2000cwa.googlegrou ps.com...
Thanks for stating that what I wanted to do is not possible. However,
saying that 'the attempt doesn't make sense' assumes knowledge about
the actual problem on which the supplied trivial was based.


Don't worry about him... he's just being an arrogant ass.
Oct 24 '05 #8

P: n/a

Mark Snelling wrote:
Thanks for stating that what I wanted to do is not possible. However,
saying that 'the attempt doesn't make sense' assumes knowledge about
the actual problem on which the supplied trivial was based.


The concept of a non-type function template makes little sense since
there is nothing that meaningfully distinguishes such a function from
an ordinary function that would make declaring one worthwhile.

Greg

Oct 24 '05 #9

P: n/a
If it makes little sense, why is it included as part of the standard?

So I can do things like this...

template< int I >
void add( int& a )
{
a += I;
}

int main( int argc, char* argv[] )
{
std::list< int > myList;
myList.push_back( 1 );
myList.push_back( 2 );
myList.push_back( 3 );
myList.push_back( 4 );

std::for_each( myList.begin(), myList.end(), add< 2 > );
std::for_each( myList.begin(), myList.end(), add< 200 > );

return 0;
}
Again, a trivial example but proves my point.

Oct 24 '05 #10

P: n/a
Mark Snelling wrote:
If it makes little sense, why is it included as part of the standard?
It's not, that's the whole point.
So I can do things like this...

std::for_each( myList.begin(), myList.end(), add< 2 > );


Since when is the third argument of std::for_each a type either?
You seem to have a fundamental problem distinguishing between types
and objects. Define a template function object and use that:

template <int I> struct add {
void operator()(int &a) {
a += I;
}
};

std::for_each(mylist.begin(), mylist.end(), add<2>());

Actually you don't even need a template really.
struct add {
int I;
add(int i) : I(i) { }
void operator() (int& a) {
a += i;
}
};

std::for_each(mylist.begin(), mylist.end(), add(2));
Oct 25 '05 #11

P: n/a
Ron, actually I understand the difference between types and object very
well. Are you saying that the example I supplied in my previous message
is invalid? Compile it and see. I think you'll find that I'm right. It
is valid code. It seems to me that you don't fully understand how you
can use the STL algorithms.

Everyone keeps getting hung up on the 'trivial' examples I'm posting.
Just because you can see a 'better' way to do what my example is doing,
doesn't mean that it fits the particular problem that I'm trying to
solve. I am well aware of using function objects, or functors, but in
my particular case they are inappropriate.

Oct 25 '05 #12

P: n/a
Mark Snelling wrote:
Ron, actually I understand the difference between types and object very
well. Are you saying that the example I supplied in my previous message
is invalid? Compile it and see. I think you'll find that I'm right. It
is valid code. It seems to me that you don't fully understand how you
can use the STL algorithms.

Everyone keeps getting hung up on the 'trivial' examples I'm posting.
Just because you can see a 'better' way to do what my example is doing,
doesn't mean that it fits the particular problem that I'm trying to
solve. I am well aware of using function objects, or functors, but in
my particular case they are inappropriate.


As long as the "'trivial' examples" you post do not parallel your problem,
you should not be surprised that the solutions proposed by the posters who
take the time of thinking through what you actually posted do not solve the
"particular case" that you keep hiding from us.

People in this group can only read what you post. Most fellows in this group
do not engage in mind reading. Post the real problem, and you might have a
shot at obtaining meaningful advice.
Best

Kai-Uwe Bux
Oct 25 '05 #13

P: n/a
I'm sure you understand that posting real problems with real code is a
violation of most companies intellectual property policies, hence my
posting of cut down examples. It is also considered bad form to post
too much code when posting in these groups.

I've actually had very helpful responses that have solved my problem
from a lot of the people here as posts to this group and private
emails. It's a shame though that there are some people that think there
is only one way to skin a cat and others that simply make arrogant
comments without providing any real assistance.

Oct 25 '05 #14

P: n/a
Mark Snelling wrote:
I'm sure you understand that posting real problems with real code is a
violation of most companies intellectual property policies, hence my
posting of cut down examples.
If you refer to something, please quote it. I said:
As long as the "'trivial' examples" you post do not parallel your problem,
you should not be surprised that the solutions proposed by the posters who
take the time of thinking through what you actually posted do not solve
the "particular case" that you keep hiding from us.
I was not suggesting to post "real code" but code that "parallels your
problem". I am sure you understand the difference.

It is also considered bad form to post too much code when posting in
these groups.
But it is necessary to post enough code to make your problem clear. And that
is not frowned upon.

I've actually had very helpful responses that have solved my problem
from a lot of the people here as posts to this group and private
emails. It's a shame though that there are some people that think there
is only one way to skin a cat and others that simply make arrogant
comments without providing any real assistance.


In case you find my comments arrogant, I apologize for hurting your
feelings.
Best

Kai-Uwe Bux
Oct 25 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.