Template typename erforderlich!?
-
Hallo!
template<typename Type, typename Allocator = std::allocator<Type> > class CSaveVector : public std::vector<Type, Allocator> { public: typedef std::vector<Type, Allocator> base_type; typedef CSaveVector<Type, Allocator> my_type; typedef base_type::size_type size_type; //Fehler: vor »CSaveVector<Type, Allocator>::base_type::size_type« ist »typename« erforderlich, da »CSaveVector<Type, Allocator>::base_type« ein abhängiger Gültigkeitsbereich ist ... };Kann mir bitte einer die Fehlermeldung erklären? (für Anfänger bitte)
- Warum ist da typename erforderlich?
- Was ist da ein abhängiger Gültigkeitsbereich? (ist doch nur eine Deklaration)
- Wie soll es laut Compiler richtig aussehen?Danke!
-
-
- Warum ist da typename erforderlich?
Weil der Compiler wissen muss, dass
base_type::size_typeüberhaupt ein Typ ist.typenameist ja ziemlich selbsterklärend
- Was ist da ein abhängiger Gültigkeitsbereich? (ist doch nur eine Deklaration)
Ein dependent name ist ein Name, der irgendwie von einem Template-Parameter (bzw. Argument) abhängt. "Abhängiger Gültigkeitsbereich" heißt, dass der Scope
CSaveVector<Type, Allocator>::base_typein der Definition vonCSaveVectorvon den Template-Parametern abhängt - nämlich von Type und Allocator:typedef std::vector<Type, Allocator> base_type;- Wie soll es laut Compiler richtig aussehen?
typedef typename base_type::size_type size_type;
-
SeppJ schrieb:
Gute Seite, die erklärt alles was ich auch gesagt habe. Nur das:
typename is prohibited in each of the following scenarios:
Outside of a template definition.Ich kann auch
int main() { typename std::string str; }schreiben.
-
Super schnell! Danke !!!
-
Sone schrieb:
SeppJ schrieb:
Gute Seite, die erklärt alles was ich auch gesagt habe. Nur das:
typename is prohibited in each of the following scenarios:
Outside of a template definition.Ich kann auch
int main() { typename std::string str; }schreiben.
Da steht auch gleich am Anfang:
Note: This page is correct (AFAIK) for C++98/03. The rules have been loosened in C++11.
-
Sone schrieb:
SeppJ schrieb:
Gute Seite, die erklärt alles was ich auch gesagt habe. Nur das:
typename is prohibited in each of the following scenarios:
Outside of a template definition.Ich kann auch
int main() { typename std::string str; }schreiben.
Das war mal richtig so. In meinem alten Draft des C++-Standards gibt es einen gestrichenen Paragraphen, der dies verbietet.
edit: Zu spät. Dafür gibt's ein Zitat des gestrichenen Absatzes 14.6-5:
The keyword typename shall only be used in template declarations and definitions, including in the return type of a
function template or member function template, in the return type for the definition of a member function of a class tem-
plate or of a class nested within a class template, and in the type-specifier for the definition of a static member of a class
template or of a class nested within a class template. The keyword typename shall be applied only to qualified names,
but those names need not be dependent. The keyword typename shall be used only in contexts in which dependent
names can be used. This includes template declarations and definitions but excludes explicit specialization declarations
and explicit instantiation declarations.