Cześć :)
Czytam ten artykuł:
http://www.parashift.com/c++-faq/nondependent-name-lookup-types.html
Za nic nie mogę zrozumieć, dlaczego autor twierdzi, że odpowiednie tam "wyrażenia" nie stanowią typów.
pozdrawiam :)
Unfortunately this doesn't work either because those names (are you ready? are you sitting down?) are not necessarily types. "Huh?!?" you say. "Not types?!?" you exclaim. "That's crazy; any fool can SEE they are types; just look!!!" you protest. Sorry, the fact is that they might not be types. The reason is that there can be a specialization of B<T>, say B<Foo>, where B<Foo>::Xyz is a data member, for example. Because of this potential specialization, the compiler cannot assume that B<T>::Xyz is a type until it knows T.
Może istnieć specjalizacja, powiedzmy B<Foo>
, gdzie B<Foo>::Xyz
to pole.
Dlatego kompilator potrzebuje wskazówki, że jest to typ.
Na ideone, dla tego kodu:
template<typename T>
class B{
public:
class Xyz{};
typedef int Pqr;
};
template<typename T>
class D : public B<T>{
public:
void g(){
B<T>::Xyz x;
}
};
otrzymuję następujący komunikat:
error: need ‘typename’ before ‘B<T>::Xyz’ because ‘B<T>’ is a dependent scope B<T>::Xyz x;
Przeciez masz tam wyraźnie to opisane.
B<T>::Xyz
Skąd wiesz że coś takiego jest typem a nie zmienną? Przecież Xyz może być zarówno klasą zagnieżdżoną jak i polem klasy B.
Denerwujące jest bardzo restrykcyjne podejście do słowa kluczowego typename
: kiedy nie jest absolutnie potrzebne to jest zakazane.
Wiem że w C++11 coś się miało w tym względzie zmienić, ale jeśli się zmieniło, to za mało.
Moim zdaniem takie coś powinno się kompilować (ale się nie skompiluje):
typename int x;