Search code examples
c++boostboost-hana

SFINAE template constructor with boost::hana


Given the following code, what is an appropriate way to express the same functionality with Boost hana?

#include <type_traits>

#include <boost/hana/type.hpp>
#include <boost/hana/core/when.hpp>
namespace hana = boost::hana;

struct S {
    template<
        typename T,
        typename = typename std::enable_if_t< (T::value) > > // <-- equivalent?
    S (const T&) { }
};

struct X { static constexpr int value = 0; };
struct Y { static constexpr int value = 1; };

int main () {
    S a (X { }); // <-- must fail
    S b (Y { });
    return 0;
}

The doc for when mention it as a replacement for enable_if but I am not sure how to apply it in this context. So, how do I selectively enable a template constructor with Boost hana?


Solution

  • As @Barry says in the comments, hana::when is useful for partial specializations, and it can't be used in your case. Hana does not provide an incantation that is more succinct than what you already have (which is fair given it's a one liner). Also note that you may drop the extra typename keyword in from of std::enable_if_t.