
hi, I have a template function <typename cT, typename tT, int n> { .... double array[n]; if (n==2) binomial code else if (n==3) trinomial code (array[0] array[1] array[2]) else .... compiler tries to compile the "trinomial code" code even when the template is instantiated with n=2, and that gives warnings because of array[2] Is there a way to make the compiler ignore the non-binomial code ? rds,

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hicham Mouline wrote: | hi, | | I have a template function <typename cT, typename tT, int n> | { | .... | double array[n]; | | if (n==2) | binomial code | else if (n==3) | trinomial code (array[0] array[1] array[2]) | else | .... | | | compiler tries to compile the "trinomial code" code even when the template | is instantiated with n=2, | and that gives warnings because of array[2] | | Is there a way to make the compiler ignore the non-binomial code ? | rds What about something like: template <int N> void n_nomial_code() {} template <> void n_nomial_code<2>() {} template <> void n_nomial_code<3>() {} ... { .... double array[n]; n_nomial_code<n>(...); ~ .... } You can pass-by-reference the data you need inside the n_nomial_code specializations. ~ Francesco. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.7 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFH3/81FCtI0YdDCEsRAk8vAJ0XELl3ODTR6LUHUVSF3CK5FgfNjgCbBKa6 PmRsXf+owWhYPJejCG8zpMY= =t0Rt -----END PGP SIGNATURE-----

AMDG Hicham Mouline wrote:
hi,
I have a template function <typename cT, typename tT, int n> { .... double array[n];
if (n==2) binomial code else if (n==3) trinomial code (array[0] array[1] array[2]) else ....
compiler tries to compile the "trinomial code" code even when the template is instantiated with n=2, and that gives warnings because of array[2]
Is there a way to make the compiler ignore the non-binomial code ? rds,
You'll have to split it out into a separate function and use template specialization/overload resolution. template<int n> void do_n_specific_code(double (&array)[n]); template<typename cT, typename tT, int n> void f() { double array[n]; do_n_specific_code(array); } In Christ, Steven Watanabe

That's what I thought... What forbids the compiler from optimizing away the test on n value, during the template instantiation process? Thanks, -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Steven Watanabe Sent: 18 March 2008 18:47 To: boost-users@lists.boost.org Subject: Re: [Boost-users] template int argument AMDG Hicham Mouline wrote:
hi,
I have a template function <typename cT, typename tT, int n> { .... double array[n];
if (n==2) binomial code else if (n==3) trinomial code (array[0] array[1] array[2]) else ....
compiler tries to compile the "trinomial code" code even when the template
is instantiated with n=2, and that gives warnings because of array[2]
Is there a way to make the compiler ignore the non-binomial code ? rds,
You'll have to split it out into a separate function and use template specialization/overload resolution. template<int n> void do_n_specific_code(double (&array)[n]); template<typename cT, typename tT, int n> void f() { double array[n]; do_n_specific_code(array); } In Christ, Steven Watanabe _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

I realize one thing more. My code refers to cT and tT template arguments: Template <typename cT, typename tT, int n> void function(const cT& o) .... double array[n]; if (n==2) binomial code depends on cT,tT (array[0] array[1]) else if (n==3) trinomial code depends on cT,tT (array[0] array[1] array[2]) else .... The "do_n_specific_code" cannot therefore be just a function... I'll take a wrapping class that includes a public static function with same signature, And the class can be partially specialized. -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Steven Watanabe Sent: 18 March 2008 18:47 To: boost-users@lists.boost.org Subject: Re: [Boost-users] template int argument AMDG Hicham Mouline wrote:
hi,
I have a template function <typename cT, typename tT, int n> { .... double array[n];
if (n==2) binomial code else if (n==3) trinomial code (array[0] array[1] array[2]) else ....
compiler tries to compile the "trinomial code" code even when the template
is instantiated with n=2, and that gives warnings because of array[2]
Is there a way to make the compiler ignore the non-binomial code ? rds,
You'll have to split it out into a separate function and use template specialization/overload resolution. template<int n> void do_n_specific_code(double (&array)[n]); template<typename cT, typename tT, int n> void f() { double array[n]; do_n_specific_code(array); } In Christ, Steven Watanabe _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Francesco Biscani
-
Hicham Mouline
-
Steven Watanabe