【发布时间】:2016-01-19 20:28:31
【问题描述】:
我想禁止为具有特定类型特征的类型实例化类模板 (PointCloud)。在以下示例中,我只想允许使用定义了 is_good 的类型:
#include <boost/core/enable_if.hpp>
class PointType1 {};
class PointType2 {};
template <typename T>
struct is_good
{
static const bool value = false;
};
template <>
struct is_good<PointType1>
{
static const bool value = true;
};
template <typename TPoint, typename boost::enable_if_c<is_good<TPoint>::value>::type = 0>
class PointCloud
{
};
int main()
{
PointCloud<PointType1> pointCloud1;
//PointCloud<PointType2> pointCloud2;
return 0;
}
错误是:
error: 'boost::enable_if_c<true, void>::type {aka void}' is not a valid type for a template non-type parameter
PointCloud<PointType1> pointCloud1;
^
据我了解,enable_if_c 应该将 ::type 定义为 TPoint,如果 is_good<TPoint>::value 是 true。如果是false,那么::type没有定义,所以SFINAE应该启动。我还以为typename会表明这确实是一个类型参数。
谁能解释为什么会这样?
【问题讨论】:
标签: c++ templates boost sfinae enable-if