C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

'compile-time if statement' using a template function

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Michael
Guest





PostPosted: Sat Apr 17, 2004 7:20 pm    Post subject: 'compile-time if statement' using a template function Reply with quote





Hi all

Is it possible to get the equivalent of a 'compile-time if statement' using
template functions? I'm guessing that template specialisation is how it
would be done, but I have no experience.

My first thought (which I don't believe works) is this:

class Base {
};

class Derived_1 : public Base {
};

class Derived_2 : public Base {
};

template <class T>
void do_it_if_D2( const Vec& v1, const Vec& v2, Vec& pt )
{
}

template <>
void do_it_if_D2<Derived_1>( const Vec& v1, const Vec& v2, Vec& pt )
{
}

template <>
void do_it_if_D2<Derived_2>( const Vec& v1, const Vec& v2, Vec& pt )
{
do_stuff_to_pt_using_v1_&_v2( v1, v2, pt );
}

....and then calling it like this.

do_it_if_D2<class_sent>( vec_a, vec_b, point );


Would be great if someone could point me in the right direction.

TIA

Michael

[excessively long sig deleted --mod]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Sun Apr 18, 2004 10:00 am    Post subject: Re: 'compile-time if statement' using a template function Reply with quote



In article <BCA5DFC2.21777%mwh_acs (AT) hotmail (DOT) com>, Michael
<mwh_acs (AT) hotmail (DOT) com> wrote:

Quote:
Hi all

Is it possible to get the equivalent of a 'compile-time if statement' using
template functions? I'm guessing that template specialisation is how it
would be done, but I have no experience.


Sure if the condition is a compile computable value.
Loki contains this
template <bool B,typename T,typename F> struct Select {typedef T
type;};
template <typename T,typename F>struct Select<false,T,F>
{
typedef F type;
};

typedef Select< bool_expr,true_type,false_type>::type result;
if bool_expr is true result is true_type else it is false_type.

Quote:
My first thought (which I don't believe works) is this:

class Base {
};

class Derived_1 : public Base {
};

class Derived_2 : public Base {
};

template <class T
void do_it_if_D2( const Vec& v1, const Vec& v2, Vec& pt )
{
}

template
void do_it_if_D2 {
}

template
void do_it_if_D2<Derived_2>( const Vec& v1, const Vec& v2, Vec& pt )
{
do_stuff_to_pt_using_v1_&_v2( v1, v2, pt );
}

...and then calling it like this.

do_it_if_D2<class_sent>( vec_a, vec_b, point );


Would be great if someone could point me in the right direction.

the easiest way is to create some tags [empty classes]

struct Derived1_tag{};
struct Derived2_tag{};
struct else_tag{};
and a conversion template compile time function:
template <class T>
struct switch_tag {typedef else_tag type;};
template <>
struct switch_tag<Derived1> {typedef Derived1_tag type;};
template <>
struct switch_tag<Derived2> {typedef Derived2_tag type;};

then provide the functions with an extra not used tag argument type,
that does the work for each case:

void do_it_if_helper(const Vec &v1,const Vec &v2,Vec &pt ,
Derived1_tag);
void do_it_if_helper(const Vec &v1,const Vec &v2,Vec &pt,
Derived2_tag);
void do_it_if_helper(const Vec &v1,const Vec &v2,Vec &pt, else_tag);

finally the template that is used to select the proper function at
compile time.

template <class T>
void do_it_if(const Vec &v1,const Vec &v2,Vec &pt)
{
return do_it_if_helper(v1,v2,pt,switch_tag<T>::type());
}

this approach is used by many of the algorithms in <algorithm> to
provide an optimzation iteatator categories via std::iterator_traits.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.