Can I tell at compile time whether a specific set of input types to a Boost.Phoenix lambda is valid?

StackOverflow https://stackoverflow.com/questions/14884789

Question

Say I have a Boost.Phoenix lambda expression, like the following:

_1 * 4.5 + (3.0 / _2)

Is there a good way to, at compile time, detect whether the lambda expression is valid, given a list of types associated with arguments _1, _2, and so on? I'm thinking that I would like to use enable_if to determine whether the input types are supported by the lambda. If so, the lambda would be invoked directly, while if not, I want to generate a no-op. Pseudocode would be something like this:

template <typename F, typename A1, typename A2>
enable_if<F::is_supported<A1, A2>, result_of<F(A1,A2)>::type >::type func(F func, A1 arg1, A2 arg2)
{
    func(A1, A2);
}

template <typename F, typename A1, typename A2>
disable_if<F::is_supported<A1, A2> >::type func(F func, A1 arg1, A2 arg2)
{
}

I'm just not sure how is_supported would be implemented, or whether it's even possible. I'm not using C++11; I would assume C++03 only (since Phoenix requires it).

Was it helpful?

Solution

I don't think it's possible in Phoenix as it stands today. To support this use, enable_if would have had to have been used on virtually every function call Phoenix makes in the eval call chain, at every level, even in the other libraries Phoenix depends on. I know Phoenix isn't written that way, and its hard to imagine that any amount of hacking on the code could make it so, before Phoenix, Proto, and all the libraries they depend on are rewritten in pure C++11. Even then, it would require great care that all potential errors appear in a SFINAE content.

I just don't see it happening, sorry.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top