|
Boost Users : |
From: Geoffrey Romer (geoff.romer_at_[hidden])
Date: 2005-10-04 13:06:47
I'm struggling with a problem of how to correctly use conditionals in
MPL. In a nutshell, the problem is that the compiler greedily
evaluates all parts of a conditional, even the branch that isn't
followed. This is a problem not just for performance, but because
sometimes the unused branch might not compile.
I know that eval_if is supposed to address this problem, but unless
I'm missing something, it doesn't seem to be a complete solution. It
delays evaluating the branches until one of them is selected, but the
compiler still evaluates the *arguments* to both branches. If the
arguments themselves don't compile, I'm screwed, despite the fact that
the parts of my code that are actually relevant (the branches that get
used) are perfectly correct.
How do I resolve this problem?
Here's a toy example, a metafunction which, given a map, finds the
value associated with the key "char" and, if it exists, increments the
associated value (which we assume is numeric):
template <typename m_map>
class toy_metafunction
{
typedef typename
if_<typename has_key<m_map, char>::type,
typename insert<m_map, pair<char,
typename plus<int_<1>,
at<m_map, char>
>::type
>
>::type,
m_map
>::type type;
};
typedef typename toy_metafunction<map< > >::type result;
This does not compile, because the compiler attempts to evaluate
plus<int_<1>, void_>. Notice that replacing if_ with eval_if does not
help, because it's not the evaluation of insert that's the problem.
Thanks in advance for any help.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net