"Mohammad" <mj*******@hotm ail.com> wrote...
"Jonathan Turkanis" <te******@kanga roologic.com> wrote in message
news:<c0******* ******@ID-216073.news.uni-berlin.de>...
"Jonathan Turkanis" <te******@kanga roologic.com> wrote in message:
"Mohammad" <mj*******@hotm ail.com> wrote in message
news:3d******** *************** ***@posting.goo gle.com...
> Hi,
>
> Is it possible to disable a method of a template class depending
on > the typename at compile time?
Now, derived<T> has a member function 'f' only if T is a pointer type.
I have used the compile-time 'if' construct mpl::if_ from the Boost
Metaprogramming Library (MPL), and the traits template is_pointer from
Boost Type Traits.
Jonathan
Thanks Jonathan!
What does the metaprogramming stand for? Can someone explain briefly.
Metaprogramming is not something that can be explained briefly. Search
the web for it.
"Meta" means "in the vicinity of". If programming is making a computer
behave in a certain way based on input data, metaprogramming is making
compiler do something based on source code (as its input data). Well,
not limited to that of course...
Compile-time calculation of certain things can be given as an example
of metaprogramming . For instance, instead of making computer calculate,
say, factorials of some numbers (which can be needed often) and instead
of precalculating them yourself and putting in a table, you could write
a metaprogram using templates to calculate it:
template<unsign ed i> struct factorial {
enum { value = i * factorial<i-1>::value }; // recursive
};
// to stop the recursion from becoming infinite
template<> struct factorial<0> { enum { value = 1 }; };
template<> struct factorial<1> { enum { value = 1 }; };
int main() {
int fact10 = factorial<10>:: value; // compiler will calculate
return 0;
}
In the example above 'value' enumerator is calculated based on recursive
instantiation of templates required and utilisation of given template
specialisations for i==1 and i==0 (just in case somebody needs it). The
compiler has to precalculate the value initialisers by instantiating all
the intermediate templates but it won't have to do that during run-time,
and there is no enormous table to type (and make a mistake in).
Victor