【发布时间】:2017-01-18 16:09:05
【问题描述】:
在像非特化模板结构pointer_traits(即template <class Ptr> struct pointer_traits)这样的类型中,存在一个成员别名模板rebind,如果它存在,则定义为Ptr::rebind<U>,或者其他类型。虽然我已经看到了一些关于检查某个成员是否存在的答案,但是如何实现像pointer_traits::rebind 这样的“条件”别名模板?也就是说,就好像通过下面的伪C++:
template <typename T> using type = has_type<T::U> ? int : float;
或
template <typename T> using type = if_has_type<T::U, int, float>::type;
我考虑使用类似于https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Member_Detector(“检测成员类型”一节)中描述的方法,但我不知道如何实现一个辅助结构,其 [sole] 成员类型取决于另一个成员类型的存在.
【问题讨论】:
标签: c++ templates template-meta-programming sfinae