473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.

Oct 24 '05 #1
14 1603
"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
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
Thanks, I didn't realise you couldn't typedef a function.

Oct 24 '05 #4

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

"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

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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Arne Petersen | last post by:
Hy, I've got a problem with member function templates compiled into libraries. I'm trying to get a library collection (coded for GNU gcc, where its compiled completly) being compiled on Visual...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
21
by: Mark Piffer | last post by:
Does a typedef like typedef sig_atomic_t atomic_int; produce an atomically write/readable type? From what I read in the standard I would guess yes, as sig_atomic_t itself is produced by a typedef...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
7
by: quarup | last post by:
I want to specialize a template function that lives inside a class, but am getting a compile error in VS.net 2003. Here's my code: template <class T> class A { public: template <class U> void...
50
by: LaundroMat | last post by:
Suppose I have this function: def f(var=1): return var*2 What value do I have to pass to f() if I want it to evaluate var to 1? I know that f() will return 2, but what if I absolutely want to...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
8
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.