我正在查看 boost units 库,我很困惑为什么 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;
在这种情况下,“启用”的作用是什么?
最佳答案
那个类模板是forward declared in "units_fwd.hpp"
.
template<class Dim,class System, class Enable=void> class unit;
我们看到第三个参数默认为 void
.我什至会想做 template<class Dim,class System, class=void> class unit;
并在实现中将其命名为无名,但可能存在编译器兼容性或代码标准,这意味着它们将其命名为 Enable
.
这种“额外”类型允许对前两种类型进行 SFINAE 测试。如果您创建一个特化,其中第三种类型仅对 Dim
的某些子集有效和 System
额外的参数(默认为 void
)可以让你做到这一点。
您甚至可以将基本实现保留为空(;
或 {};
,它们具有不同的效果)并且仅专门化,使测试失败的参数被视为无效选项。
这是 SFINAE 的玩具示例:
template<class T, class=void> struct is_int : std::false_type {};
template<class T> struct is_int< T,
std::enable_if_t< std::is_same<T, int>{} >
> : std::true_type {};
基础特化继承自false_type
.但是,如果类型 T
下个专业通过我的考试,优先,结果继承自true_type
.
在这种情况下,我们最好只做 is_int< int, void >:std::true_type{}
, 但在更复杂的情况下(例如 is_integral
)我们可以进行几乎任意的编译时检查并使用它来启用或禁用 true_type
特化。
关于c++ - 为什么模板类会有未使用的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28751940/