why __dependent_type from libcxx uses a template non-type parameter bool _Dummy?

旧时模样 提交于 2019-12-11 05:23:10

问题


Here is the definition of __dependent_type:

template <class _Tp, bool>
struct _LIBCPP_TEMPLATE_VIS __dependent_type : public _Tp {};

All use cases:

/usr/.../c++/v1 >>> rg "_dependent_type"                                                                                                                      
memory
2211:          __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2212:          __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2402:      typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
2406:      typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
2410:      typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
2412:  template <bool _Dummy, class _Deleter = typename __dependent_type<
2671:      typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
2675:      typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
2679:      typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
2681:  template <bool _Dummy, class _Deleter = typename __dependent_type<

variant
1134:            enable_if_t<__dependent_type<is_default_constructible<__first_type>,
1292:              __dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
1293:              __dependent_type<is_swappable<_Types>, _Dummy>::value)...>::value,

tuple
621:            __lazy_all<__dependent_type<is_default_constructible<_Tp>, _Dummy>...>

Question:

Isn't

template <class _Tp>
struct _LIBCPP_TEMPLATE_VIS __dependent_type : public _Tp {};

and

 __dependent_type<is_default_constructible<_T1>>::value

enough?

Why does libcxx need this dummy bool?


回答1:


It is the dummy bool that makes the type dependent, this is the whole point of __dependent_type, otherwise you can just use the type itself.

Take this code as example:

  template <bool _Dummy>
  using _GoodRValRefType =
      typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;

Without the dummy to make it a dependent type, when the class template gets instantiated, _DeleterSFINAE::__good_rval_ref_type might cause a hard error, because not all of the _DeleterSFINAE has a __good_rval_ref_type member.

The dependent type delays evaluation, so that you can use _GoodRValRefType in a SFINAE context later.



来源:https://stackoverflow.com/questions/53901072/why-dependent-type-from-libcxx-uses-a-template-non-type-parameter-bool-dummy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!