Avendo codice come questo
codice:
template<class T>
 std::list<T>::iterator nomefunzione()
{
   std::list<T> _list;
   //.. codice
   return _list.begin();
}
ottengo un errore dal compialtore (sto lavorando con visual studio 2012)
warning C4346: 'std::list<T>::iterator': il nome dipendente non è un tipo
Andando in giro a cercare su internet ho scoperto che per nona vere questo errore devo sempre premettere la keyword typename .
Effettivamente riscrivendo la funzione come segue non ottengo errori:
codice:
template<class T>
 typename std::list<T>::iterator nomefunzione()
{
   std::list<T> _list;
   //.. codice
   return _list.begin();
}
Francamente l'unica spiegazione che ho trovato mi risulta aimè molto ostica e non riesco realmente a comprenderla
The point is that you have a template that uses another template based on its own template parameters. That means that if e.g. the other class is specialised for a certain type, it could change the whole interface when compared to the general template, including sometimes twisting around the meaning of things.
Example: under weird circumstances, e.g. list<weird_thing>::iterator might
in fact refer to a static member. Not that there is any reason to do just
that with list<>, but it's not up to the compiler to reason. Therefore,
when referring to a nested type or typedef, you need to prefix it
with 'typename' in this context.
Inoltre ho un altro problema:
Nel file che sto scrivendo list::iterator appare in numerosi punti, avevo quindi pensato di usare una comoda typedef, come potrebbe essere questa
codice:
template<class T> typedef std::list<T>::iterator listIterator<T>;
ovviamente non compila e da lo stesso problema di cui sopra.. in questo caso non posso però premettere la keyword typename : esistono altri metodi per ottenere lo stesso risultato?