*
s とs の数に関係なく、基本型を返すメタ関数を作成するのはそれほど難しくありません&
。
template <typename T>
struct remove_all_ref_ptr { typedef T type; };
template <typename T>
struct remove_all_ref_ptr<T *> : public remove_all_ref_ptr<T> { };
template <typename T>
struct remove_all_ref_ptr<T * const> : public remove_all_ref_ptr<T> { };
template <typename T>
struct remove_all_ref_ptr<T * volatile> : public remove_all_ref_ptr<T> { };
template <typename T>
struct remove_all_ref_ptr<T * const volatile> : public remove_all_ref_ptr<T> { };
template <typename T>
struct remove_all_ref_ptr<T &> : public remove_all_ref_ptr<T> { };
template <typename T>
struct remove_all_ref_ptr<T &&> : public remove_all_ref_ptr<T> { };
そして::std::remove_pointer
、そのようなタイプをうまく処理できます。それは単一の*
.
先ほど書いた小さなメタ関数は、最終的に再帰の深さの制限に達します。しかし、それを実現するには、おそらく 100 を超える費用を負担する必要があります*
。
const および volatile 修飾子も削除するために、標準ライブラリのメタ関数と組み合わせて使用する方法を次に示します。
#include <type_traits>
template <typename T>
struct base_type : public ::std::remove_cv<typename remove_all_ref_ptr<T>::type> { };