开发者

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开发者_JS百科?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜