【发布时间】:2015-02-26 20:17:29
【问题描述】:
我正在查看 boost 单元库,我很困惑为什么 boost::units::unit 类有一个额外的模板参数。示例如下:
http://www.boost.org/doc/libs/1_57_0/boost/units/unit.hpp
template<class Dim,class System, class Enable>
class unit
{
public:
typedef unit<Dim, System> unit_type;
typedef unit<Dim,System> this_type;
typedef Dim dimension_type;
typedef System system_type;
unit() { }
unit(const this_type&) { }
//~unit() { }
this_type& operator=(const this_type&) { return *this; }
// sun will ignore errors resulting from templates
// instantiated in the return type of a function.
// Make sure that we get an error anyway by putting.
// the check in the destructor.
#ifdef __SUNPRO_CC
~unit() {
BOOST_MPL_ASSERT((detail::check_system<System, Dim>));
BOOST_MPL_ASSERT((is_dimension_list<Dim>));
}
#else
private:
BOOST_MPL_ASSERT((detail::check_system<System, Dim>));
BOOST_MPL_ASSERT((is_dimension_list<Dim>));
#endif
};
该类用于向维度系统添加维度。
typedef unit<pressure_dimension,si::system> pressure;
在这种情况下,“启用”的作用是什么?
【问题讨论】:
-
可能是某种 SFINAE?
-
@black 像
Enable这样的名字我也猜到了。 -
使用
enable_if获得 SFINAE 杠杆作用。见example
标签: c++ templates generic-programming template-classes boost-units