 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gioele Barabucci Guest
|
Posted: Wed Jul 21, 2004 7:58 pm Post subject: Templates vs. params |
|
|
What advantages has
template <int N> int foo(int a) { return a * N; }
over
int foo(int a, int n) { return a * n; }
??
The former can be optimized at compile time while the latter can't (or,
better, could be optimized but rarely is).
Are there other advantages of templates that I'm not aware of?
PS.: where can I find infos about the implementation of C++'s templates
(or similar constructs in other languages)?
--
Gioele Barabucci <barabucc (AT) cs (DOT) unibo.it>
) http://cs.unibo.it/~barabucc
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ali Cehreli Guest
|
Posted: Thu Jul 22, 2004 11:12 am Post subject: Re: Templates vs. params |
|
|
On Wed, 21 Jul 2004 12:58:25 -0700, Gioele Barabucci wrote:
| Quote: | What advantages has
template <int N> int foo(int a) { return a * N; }
over
int foo(int a, int n) { return a * n; }
??
|
For the template, you must know the value of N at compile time, and
call the function with that value like
foo<7>(var);
For the regular function, 'n' can have any value either known at
compile time or acquired at runtime:
foo(var, 7);
foo(var, some_value);
| Quote: | The former can be optimized at compile time while the latter can't (or,
better, could be optimized but rarely is).
|
The template version is likely to be quicker because the
implementation must be visible at compile time, giving it more
optimization chances.
In the case of the regular function, it can not be optimized if the
implementation is not visible at the call site. The 'n' may need to be
copied to the implementation before use (most likely on program
stack).
If the implementation is visible to the compiler though, then the two
functions have the same optimization chances.
| Quote: | Are there other advantages of templates that I'm not aware of?
|
Templates save us from redefining the same data structures and
algorithms over and over for different types. The following class can
work with any type that can be divided by two.
template <class Type>
class Data
{
Type data_;
public:
explicit Data(Type data)
:
data_(data)
{}
Type half() const
{
return data_ / 2;
}
};
#include <iostream>
int main()
{
Data<double> d(1. ;
Data<int> i(42);
std::cout << d.half() << ' ' << i.half() << 'n';
}
Check out the standard library, which is full of class and function
templates like std::vector, std::for_each, etc.
There are many interesting techniques that use templates. You can look
at Modern C++ Design by Andrei Alexandrescu for very advanced examples
of these.
Ali
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pavel Vozenilek Guest
|
Posted: Thu Jul 22, 2004 11:13 am Post subject: Re: Templates vs. params |
|
|
"Gioele Barabucci" wrote:
| Quote: | Are there other advantages of templates that I'm not aware of?
Quite a few. See link bellow. |
| Quote: |
PS.: where can I find infos about the implementation of C++'s templates
(or similar constructs in other languages)?
This book covers templates into depth and has beginners intro too: |
http://www.vandevoorde.com/Templates/
/Pavel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ilmari Krebs Guest
|
Posted: Thu Jul 22, 2004 10:23 pm Post subject: Re: Templates vs. params |
|
|
| Quote: |
PS.: where can I find infos about the implementation of C++'s
templates
|
C++ Templates The complete guide, Vavid Vandevoorde, Nicolai M. Josuttis, Addison Wesley
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gioele Barabucci Guest
|
Posted: Thu Jul 22, 2004 10:46 pm Post subject: Re: Templates vs. params |
|
|
Il 22 Jul 2004 07:13:19 -0400, Pavel Vozenilek scriveva:
| Quote: | "Gioele Barabucci" wrote:
PS.: where can I find infos about the implementation of C++'s templates
(or similar constructs in other languages)?
This book covers templates into depth and has beginners intro too:
http://www.vandevoorde.com/Templates/
I was refering to 'compile side' implemetation  |
What techniques are there to implement templates on compilers?
--
Gioele Barabucci <barabucc (AT) cs (DOT) unibo.it>
) http://cs.unibo.it/~barabucc
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pavel Vozenilek Guest
|
Posted: Fri Jul 23, 2004 11:53 pm Post subject: Re: Templates vs. params |
|
|
"Gioele Barabucci" wrote:
| Quote: | PS.: where can I find infos about the implementation of C++'s
templates
(or similar constructs in other languages)?
This book covers templates into depth and has beginners intro too:
http://www.vandevoorde.com/Templates/
I was refering to 'compile side' implemetation
What techniques are there to implement templates on compilers?
Oh so. Then maybe GCC source. |
/Pavel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|