Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove reference with const references

For a parametric class C, I want to get always the "primitive" type irrespective of pointer, const or reference modifiers.

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

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

For example, for some_type equal to:

  • int&
  • int**
  • int*&
  • int const &&
  • int const * const
  • and so on

I want a is always of type int. How can I achieve it?

like image 649
Peregring-lk Avatar asked Jan 25 '13 13:01

Peregring-lk


1 Answers

If you want to use the standard library more, you can do:

#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; };

which removes stuff until that doesn't change the type anymore. With a more recent standard, this can be shortened to

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;
like image 134
Marc Glisse Avatar answered Oct 11 '22 21:10

Marc Glisse