使用const引用删除引用

时间:2013-01-25 13:11:18

标签: c++ templates c++11 std

对于参数类C,我想总是得到“原始”类型,而不管指针,const或引用修饰符。

template<typename __T>
class C
{
public:
    typedef std::some_magic_remove_all<__T>::type T;
}

int main()
{
    C<some_type>::type a;
}

例如,some_type等于:

  • int&
  • int**
  • int*&
  • int const &&
  • int const * const

我希望a始终为int类型。我怎样才能实现它?

3 个答案:

答案 0 :(得分:11)

template<class T> struct remove_all { typedef T type; };
template<class T> struct remove_all<T*> : remove_all<T> {};
template<class T> struct remove_all<T&> : remove_all<T> {};
template<class T> struct remove_all<T&&> : remove_all<T> {};
template<class T> struct remove_all<T const> : remove_all<T> {};
template<class T> struct remove_all<T volatile> : remove_all<T> {};
template<class T> struct remove_all<T const volatile> : remove_all<T> {};
//template<class T> struct remove_all<T[]> : remove_all<T> {};
//template<class T, int n> struct remove_all<T[n]> : remove_all<T> {};

我最初也剥离了范围(数组),但约翰尼斯注意到这会导致const char[]的含糊不清,问题并没有提到它们。如果我们也想剥离数组(参见评论中提到的想法),以下内容并没有太多复杂化:

#include <type_traits>
template<class U, class T = typename std::remove_cv<U>::type>
struct remove_all { typedef T type; };
template<class U, class T> struct remove_all<U,T*> : remove_all<T> {};
template<class U, class T> struct remove_all<U,T&> : remove_all<T> {};
template<class U, class T> struct remove_all<U,T&&> : remove_all<T> {};
template<class U, class T> struct remove_all<U,T[]> : remove_all<T> {};
template<class U, class T, int n> struct remove_all<U,T[n]> : remove_all<T> {};

或使用辅助类但只有一个模板参数:

#include <type_traits>
template<class T> struct remove_all_impl { typedef T type; };
template<class T> using remove_all =
  remove_all_impl<typename std::remove_cv<T>::type>;
template<class T> struct remove_all_impl<T*> : remove_all<T> {};
template<class T> struct remove_all_impl<T&> : remove_all<T> {};
template<class T> struct remove_all_impl<T&&> : remove_all<T> {};
template<class T> struct remove_all_impl<T[]> : remove_all<T> {};
template<class T, int n> struct remove_all_impl<T[n]> : remove_all<T> {};

如果所有变体开始寻找相同的情况,这是正常的; - )

答案 1 :(得分:11)

如果您想更多地使用标准库,您可以这样做:

#include <type_traits>
template<class T, class U=
  typename std::remove_cv<
  typename std::remove_pointer<
  typename std::remove_reference<
  typename std::remove_extent<
  T
  >::type
  >::type
  >::type
  >::type
  > struct remove_all : remove_all<U> {};
template<class T> struct remove_all<T, T> { typedef T type; };

除去东西,直到不再改变类型。使用更新的标准,可以缩短为

template<class T, class U=
  std::remove_cvref_t<
  std::remove_pointer_t<
  std::remove_extent_t<
  T >>>>
  struct remove_all : remove_all<U> {};
template<class T> struct remove_all<T, T> { typedef T type; };
template<class T> using remove_all_t = typename remove_all<T>::type;

答案 2 :(得分:1)

您还可以使用remove_cvref_t函数,该函数自c ++ 20起就可用

#include <iostream>
#include <type_traits>


int main()
{
    std::cout << std::boolalpha
              << std::is_same_v<std::remove_cvref_t<int>, int> << '\n'
              << std::is_same_v<std::remove_cvref_t<int&>, int> << '\n'
              << std::is_same_v<std::remove_cvref_t<int&&>, int> << '\n'
              << std::is_same_v<std::remove_cvref_t<const int&>, int> << '\n'
              << std::is_same_v<std::remove_cvref_t<const int[2]>, int[2]> << '\n'
              << std::is_same_v<std::remove_cvref_t<const int(&)[2]>, int[2]> << '\n'
              << std::is_same_v<std::remove_cvref_t<int(int)>, int(int)> << '\n';
}