template related question

J

Jonathan Turkanis

Mohammad said:
Hi,

Is it possible to disable a method of a template class depending on
the typename at compile time?

In many cases it is possible to keep certain templated functions from
being considered during overload resolution, depending on properties
of the candidate template arguments. This uses the enable_if utility,
now part of boost:

http://www.boost.org/libs/utility/enable_if.html

Note that it requires compiler support for the SFINAE principle, and
therefore will not work on many old compilers, and some new ones.

Jonathan
 
J

Jonathan Turkanis

Sorry to reply to myself. I noticed that in your question the class is
a template, but not necessarily the member function. In this case, you
often don't need SFINAE, or even partial specialization.

Example:

struct base_with_f {
void f() { }
};

struct base_without_f { };

template<typename T>
struct derived
: mpl::if_<
is_pointer<T>,
base_with_f,
base_without_f
{ };

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
 
M

Mohammad

Hi,

Is it possible to disable a method of a template class depending on
the typename at compile time?

thanks!
 
R

Rob Williscroft

Mohammad wrote in
Hi,

Is it possible to disable a method of a template class depending on
the typename at compile time?

thanks!

Yes, look for restrict_to (and enable_if)

Google groups:
http://groups.google.co.uk/groups?q=restrict_to&ie=UTF-8&oe=UTF-8&hl=en

boost:
http://boost-consulting.com/boost/libs/utility/enable_if.html

The other simpler way is just to intrudce an error.

template < typename > struct helper;

tempate <> struct helper< int > { typedef int type; };
tempate <> struct helper< double > { typedef int type; };


template < typename T >
struct example
{
T method()
{
typedef typename helper< T >::type T_is_wrong_type;
return T();
}
};

any call to example< T >::method() should genarate a compile error
unless T is an int or a double.

Rob.
 
M

Mohammad

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

Victor Bazarov

Mohammad said:
"Jonathan Turkanis" <[email protected]> wrote in message



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

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
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top