| View previous topic :: View next topic |
| Author |
Message |
Olaf Petzold Guest
|
Posted: Mon Nov 03, 2003 9:45 am Post subject: Spezialisierung von Member Templates |
|
|
Hallo,
warum wirft der g++ 3.3.x hier das Handtuch?
---8<---
#include
template <unsigned Sz>
struct Foo {
enum {
big = Sz > 10 ? true : false
};
template<bool BIG>
void bar() { std::cout << "big " << Sz << std::endl; }
void bar
};
int main()
{
Foo<3> foo3;
Foo<100> foo100;
foo3.bar();
foo100.bar();
}
--->8---
Er meint:
bar.cc:12: template-id `sorry, not implemented: `component_ref' not
supported by dump_decl
<declaration error><false>' in declaration of primary template
Erlaubt der Standard dieses und es ist nicht im g++ implementiert? Wie kann
ich es umgehen?
Vielen Dank
Olaf
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Jan Langer Guest
|
Posted: Mon Nov 03, 2003 10:06 am Post subject: Re: Spezialisierung von Member Templates |
|
|
Olaf Petzold wrote:
| Quote: | template <unsigned Sz
struct Foo {
enum {
big = Sz > 10 ? true : false
};
template<bool BIG
void bar() { std::cout << "big " << Sz << std::endl; }
void bar
};
|
(member) funktionen kann man nicht spezialisieren, nur ueberladen.
in diesem fall kannst du eine helper klasse schreiben, welche du dann
spezialisierst. der trick ist dann aus der funktion die aufgabe an diese
helper-klasse weiterzuleiten.
template
struct bar_helper
{
void operator () () const
{
std::cout << "big " << Sz << std::endl;
}
};
template
struct bar_helper <false, Sz>
{
void operator () () const
{
std::cout << "small " << Sz << std::endl;
}
};
template
struct foo
{
enum {
big = Sz > 10 ? true : false
};
void bar ()
{
bar_helper <big, Sz> bh;
bh ();
}
};
jan
--
jan langer ... [email]jan (AT) langernetz (DOT) de[/email]
"pi ist genau drei"
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Markus Breuer Guest
|
Posted: Mon Nov 03, 2003 12:51 pm Post subject: Re: Spezialisierung von Member Templates |
|
|
Olaf Petzold schrieb:
| Quote: | Hallo,
warum wirft der g++ 3.3.x hier das Handtuch?
---8<---
#include
template
struct Foo {
enum {
big = Sz > 10 ? true : false
};
template<bool BIG
void bar() { std::cout << "big " << Sz << std::endl; }
void bar
};
int main()
{
Foo<3> foo3;
Foo<100> foo100;
foo3.bar();
foo100.bar();
}
--->8---
Er meint:
bar.cc:12: template-id `sorry, not implemented: `component_ref' not
supported by dump_decl
declaration error><false>' in declaration of primary template
Erlaubt der Standard dieses und es ist nicht im g++ implementiert? Wie kann
ich es umgehen?
|
Meiner Meinung nach machst du die template-Spezialisierung nicht
richtig. Versuche es mal so:
template<bool BIG>
void bar() { std::cout << "big " << Sz << std::endl; }
template<> // diese Zeile fehlte dir
void bar<false>() { std::cout << "small " << Sz << std::endl; }
Gruß Markus
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Olaf Petzold Guest
|
Posted: Mon Nov 03, 2003 1:08 pm Post subject: Re: Spezialisierung von Member Templates |
|
|
| Quote: | Meiner Meinung nach machst du die template-Spezialisierung nicht
richtig. Versuche es mal so:
template<bool BIG
void bar() { std::cout << "big " << Sz << std::endl; }
template<> // diese Zeile fehlte dir
void bar<false>() { std::cout << "small " << Sz << std::endl; }
|
Dann kommt die Meldung:
bar.cc:12: explicit specialization in non-namespace scope `struct Foo
bar.cc:12: enclosing class templates are not explicitly specialized
bar.cc:13: syntax error before `{' token
bar.cc:14: parse error before `}' token
bar.cc: In function `int main()':
bar.cc:22: no matching function for call to `Foo<3>::bar()'
bar.cc:23: no matching function for call to `Foo<100>::bar()'
Scheint für mich also ein Syntax Fehler zu sein, sehen kann ich leider
nicht.
Vielen Dank
Olaf
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Mon Nov 03, 2003 1:52 pm Post subject: Re: Spezialisierung von Member Templates |
|
|
Olaf Petzold wrote:
| Quote: | Meiner Meinung nach machst du die template-Spezialisierung nicht
richtig. Versuche es mal so:
template<bool BIG
void bar() { std::cout << "big " << Sz << std::endl; }
template<> // diese Zeile fehlte dir
void bar<false>() { std::cout << "small " << Sz << std::endl; }
Dann kommt die Meldung:
bar.cc:12: explicit specialization in non-namespace scope `struct
Foo
bar.cc:12: enclosing class templates are not explicitly
specialized
|
Das sagt doch schon recht eindeutig, was das Problem ist. Du kannst
Memberfunktionen nicht einzeln spezialisieren. Du mußt immer die ganze
Klasse spezialisieren.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Daniel Albuschat Guest
|
Posted: Tue Nov 04, 2003 7:30 am Post subject: Re: Spezialisierung von Member Templates |
|
|
Olaf Petzold wrote:
| Quote: | warum wirft der g++ 3.3.x hier das Handtuch?
Der Standard sieht dies nicht vor.
---8<---
#include
template
struct Foo {
enum {
big = Sz > 10 ? true : false
};
template<bool BIG
void bar() { std::cout << "big " << Sz << std::endl; }
Hier fehlte ein template<>, damit es wie eine partielle Spezialisierung |
aussieht. Funktioniert aber trotzdem nicht.
| Quote: | void bar<false>() { std::cout << "small " << Sz << std::endl; }
};
int main()
{
Foo<3> foo3;
Foo<100> foo100;
foo3.bar();
foo100.bar();
Welches bar() soll er hier aufrufen? Das musst du ihm schon mitteilen.
}
--->8---
Erlaubt der Standard dieses und es ist nicht im g++ implementiert?
|
Der Standard erlaubt es nicht.
| Quote: | Wie kann ich es umgehen?
|
Mit einer Technik, die in Andrei Alexandrescus "Modern C++ Design"
beschrieben ist:
Man macht aus einer partiellen Spezialisierung eine Ueberladung, indem
man einen Dummy-Parameter einbaut. Fuer Partielle Spezialisierung nach
Wert muss man das allerdings ein wenig abwandeln. Das sieht dann so aus
(der Einfachheit halber ist Foo kein Template mehr):
template<bool T>
struct boolval2type;
template<> struct boolval2type<true>{ };
template<> struct boolval2type<false>{ };
struct Foo {
template<bool BIG>
void bar() {
bar_helper( boolval2type<BIG>() );
}
private:
void bar_helper( boolval2type<true> ) {
std::cout << "truen";
}
void bar_helper( boolval2type
std::cout << "falsen";
}
};
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
|