使用boost::hana的SFINAE模板构造函数

SFINAE template constructor with boost::hana

本文关键字:SFINAE 构造函数 boost hana 使用      更新时间:2023-10-16

给定以下代码,用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;
}

when的文档提到它作为enable_if的替代品,但我不确定如何在这种情况下应用它。那么,如何有选择地使用Boost hana启用模板构造函数呢?

正如@Barry在评论中所说,hana::when对于部分专门化是有用的,而它不能用于您的情况。Hana没有提供比您已有的咒语更简洁的咒语(考虑到它是一行字,这是公平的)。还要注意,您可以从std::enable_if_t中删除额外的typename关键字。