How do you typdef a function template

M

Mark Snelling

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

John Carson

Mark Snelling said:
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>();
}
 
J

Jonathan Mcdougall

Mark said:
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
 
G

Greg

Mark said:
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
 
K

Kai-Uwe Bux

Mark said:
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
 
M

Mark Snelling

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

Jon Slaughter

Mark Snelling said:
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.
 
G

Greg

Mark said:
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
 
M

Mark Snelling

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

Ron Natalie

Mark said:
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));
 
M

Mark Snelling

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

Kai-Uwe Bux

Mark said:
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
 
M

Mark Snelling

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

Kai-Uwe Bux

Mark said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top