0

In STL, I saw the following template definitions:

template <class _Ty>
struct remove_reference {
    using type                 = _Ty;
    using _Const_thru_ref_type = const _Ty;
};

template <class _Ty>
struct remove_reference<_Ty&> {
    using type                 = _Ty;
    using _Const_thru_ref_type = const _Ty&;
};

I have not seen struct remove_reference<_Ty&> this kind of syntax before at the definition of a struct. The syntax similar to this that I know is that when you create a new object e.g. remove_reference<_Ty&> newObject.

What does it mean to use struct remove_reference<_Ty&> at the definition of a struct?

This is not like a template specialization either, as it will resemble the format like template<> struct remove_reference<_Ty&>. Could anyone let me know what c++ feature this is, and the official link, please?

Please note: this is not a duplicate of the common specification questions. As they didn't discuss the syntax of keeping both the generic template parameter on the top and in the struct at the same time.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Anning Wuwang
  • 333
  • 1
  • 8
  • 3
    Please look for the term *specialization*. – Some programmer dude Dec 12 '22 at 14:41
  • 2
    more specifically [partial template specialization](https://en.cppreference.com/w/cpp/language/partial_specialization) – 463035818_is_not_an_ai Dec 12 '22 at 14:43
  • Thanks guys, for template specialization does it take the form of template<> struct remove_reference<_Ty&> instead of template struct remove_reference<_Ty&>? – Anning Wuwang Dec 12 '22 at 14:47
  • @AnningWuwang -- no, that's what you do when you have a **specific** type: `template <> struct X ...`; the compiler knows what `int` means. When you want to use a **generic** type with a qualifier you have to have a name for the generic type, hence, `template struct X ...`. If you write `template <> struct X...` the compiler doesn't know what the name `Ty` refers to. – Pete Becker Dec 12 '22 at 14:54
  • I see, thank you @PeteBecker! Do you have any official links say a link on cppreference.com introducing this syntax I can have a good read about? – Anning Wuwang Dec 12 '22 at 15:15

0 Answers0