mercredi 6 décembre 2017

How to declare pointers/references to template class member types?

I hope the title is not too far off. Consider the following piece of code:

template<class C>
struct Wrapper {
  using type = C;
};

int main()
{
  Wrapper<int>::type *a;
  Wrapper<int*>::type b;
  int i;
  Wrapper<int>::type &c = i;
  Wrapper<int&>::type d = i;
}

Here, * and & can either go into the template argument or can be attached to the variable name. While I think both ways of declaring have the same effect, this is just my question:

Are the different ways of declaring pointers/references equivalent or are there situations in which one of them is preferred over the other for whatever reasons (e.g. safer, compiles faster, better style, ...)?

Aucun commentaire:

Enregistrer un commentaire