1

Today I was digging through the source code of the Visual Studio C++ implementation and stumbled across the following lines of the std::unique_ptr:

template<class _Ty,class _Dx>
    class unique_ptr<_Ty[], _Dx>

I understand the first line. The second line surprised me. Why are the template arguments behind the name of the class? What does that mean? Probably it has to do with the fact that this is the array variant of unique_ptr?

Brotcrunsher
  • 1,964
  • 10
  • 32
  • 1
    http://en.cppreference.com/w/cpp/language/partial_specialization – chris Oct 05 '17 at 13:14
  • This is a partial specialization. – Jodocus Oct 05 '17 at 13:14
  • Please research before asking. If you are just learning c++, consider reading [one of these books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Passer By Oct 05 '17 at 13:25
  • @PasserBy I am not just learning C++, I have read several of these books already. Yet i never seen partial spezialization before. I have searched for it, too. But google did not anwer my question. – Brotcrunsher Oct 05 '17 at 13:28
  • @Brotcrunsher I find that incredible. If I searched "c++ template", among Google's answers are "What is template specialization?". If you looked through cppreference's article about templates, you will inevitably find the noun "specialization". – Passer By Oct 05 '17 at 13:32
  • @PasserBy I know template specialization so I must have overlook the partial part. Hope I didn't waste your time too badly. – Brotcrunsher Oct 05 '17 at 13:36

1 Answers1

3

The the primary template of std::unique_ptr looks like the following:

template<class _Ty, class _Dx>
class unique_ptr {
    /* ... */
};

The template above works for any type except for arrays. Therefore the std::unique_ptr has a partial specialization for arrays which looks like:

template<class _Ty, class _Dx>
class unique_ptr<_Ty[], _Dx> {
    /* ... */
};

When the compiler encounters an instantiation of the mentioned template e.g. as

std::unique_ptr<int[]> foo;

it will use the specialized template instead of primary template.

Akira
  • 4,385
  • 3
  • 24
  • 46