mercredi 8 avril 2020

decltype on an element of std::vector

Why does the following not compile?

  std::vector<int> v{1,2};
  decltype(v[0]) i;           //doesn't work
  decltype(v)::value_type j;  //works

I receive the error test.cpp:31:18: error: declaration of reference variable 'i' requires an initializer. Isn't v[0] of type int here?

I understand that even if it did work, it wouldn't be as safe as the latter in case the vector is empty, but I feel that should be a runtime issue rather than a compile time issue.

Aucun commentaire:

Enregistrer un commentaire