【问题标题】:SFINAE template constructor with boost::hana带有 boost::hana 的 SFINAE 模板构造函数
【发布时间】:2016-09-28 15:21:07
【问题描述】:

鉴于以下代码,用 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 选择性地启用模板构造函数?

【问题讨论】:

  • hana::when 是关于启用部分专业化。
  • @Barry:是的,我正在寻找一种(也许)聪明的方法,用一些简洁的hana咒语代替普通的 enable_if。

标签: c++ boost boost-hana


【解决方案1】:

正如@Barry 在 cmets 中所说,hana::when 对于部分专业化很有用,它不能用于您的情况。 Hana 没有提供比你已经拥有的更简洁的咒语(这是公平的,因为它是单行的)。另请注意,您可以从std::enable_if_t 中删除额外的typename 关键字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    相关资源
    最近更新 更多