使用boost::hana的SFINAE模板构造函数
SFINAE template constructor with boost::hana
给定以下代码,用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
关键字。
相关文章:
- 构造函数SFINAE和继承在clang中失败
- C++:SFINAE 来区分填充和范围构造函数?
- SFINAE enable_if 使用 pre c++11 启用构造函数
- 构造函数超载和Sfinae
- 可变参数构造函数中的 SFINAE
- 通常应用方法,使用带有构造函数委托的 SFINAE 通过类模板的构造函数初始化成员
- 使用Sfinae切换构造函数的麻烦
- 将 SFINAE 用于模板化构造函数时出现问题
- 构造函数上的SFINAE在VC2017中工作,但在clang / gcc中不起作用
- 构造函数参数的 SFINAE
- 变量args SFINAE默认构造函数在clang中工作,但在Visual Studio 2015中失败
- SFINAE enable_if显式构造函数
- 如何对继承构造函数执行正确的SFINAE
- 在模板类构造函数(SFINAE)中区分1D和2D容器
- 类模板构造函数中的SFINAE
- SFINAE -用一个参数检测构造函数
- 如果给定模板参数,则使用sfinae启用构造函数提供类型定义
- 在模板参数中通过SFINAE选择构造函数
- 使用boost::hana的SFINAE模板构造函数
- SFINAE条件和构造函数参数类型