Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use boost::ice_or instead of || and boost::ice_and instead of && in enable_if?

As question states, is there a reason why people use the struct version over the normal conditionals?

like image 576
Xeo Avatar asked Mar 23 '11 02:03

Xeo


1 Answers

An excerpt from the Boost Coding Guidelines for Integral Constant Expressions:

Don't use logical operators in integral constant expressions; use template meta-programming instead.

The header contains a number of workaround templates, that fulfil the role of logical operators, for example instead of:

INTEGRAL_CONSTANT1 || INTEGRAL_CONSTANT2

Use:

::boost::type_traits::ice_or<INTEGRAL_CONSTANT1,INTEGRAL_CONSTANT2>::value

Rationale: A number of compilers (particularly the Borland and Microsoft compilers), tend to not to recognise integral constant expressions involving logical operators as genuine integral constant expressions. The problem generally only shows up when the integral constant expression is nested deep inside template code, and is hard to reproduce and diagnose.

So I'd say never on a compliant compiler. (But if you need to support non-compliant compilers, use it.)

like image 93
GManNickG Avatar answered Sep 27 '22 23:09

GManNickG