1

Im trying to write a scoped pointer class which calls delete once it is destroyed. I realized that i would need to check whether my pointer is pointing to an array or not, so i could call the right delete. Taking inspiration from std::unique_ptr i used type_traits to check if the template argument, which holds the type pointer, is an array or not:

template <typename type, bool _Dx = std::is_array<type>::value>
    class scoped_ptr {
    private:
        type* m_ptr;
    //...
    };

template <typename type>
    class scoped_ptr<type, true> {};

But if my template argument type is "int[]" the codes invalidates because i cant have an "int[]* m_ptr" How can i solve this problem? How can i pass int[] argument and have "int* m_ptr"

Denomycor
  • 157
  • 6

1 Answers1

1

What you want is std::remove_extent. If you give it an array it gives you the element type, otherwise it just gives you the type you gave it. That would look like

template <typename type, bool _Dx = std::is_array<type>::value>
class scoped_ptr {
private:
    std::remove_extent_t<type>* m_ptr;
//...
};

Also note that _Dx is an illegal name. All names that start with an underscore and are followed with a capital letter are reserved for the implementation.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402