 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Wed Sep 01, 2004 2:14 pm Post subject: partial function template |
|
|
Hallo, ich versuche grade einige allgemeine Konvertierungsfunctionen
zu schreiben. Dazu versuche ich function templates zu benutzen. Leider
gibt es einige problme bei der function template Spezialisierung.
Muss ich klassen templates nehmen oder mach ich etwas anders falsch?
Gruss
Bernd
------------- Source code ----------
#include <iostream>
#include <string>
#include <boost/type_traits.hpp>
template<bool cond, class S, class D> void my_cast(const S src, D &dst)
{
D d;
dst=d;
std::cerr << "not implemented yet " << std::endl;
}
// ERROR uncommnet this function to compile
template
{
dst=static_cast<D>(src); //boost::num_cast dynamic_cast etc.
}
template<class S, class D> void convert(const S src,D &dst)
{
D d;
dst=d;
bool b= boost::is_convertible<S,D>::value;
std::cout << "Convert " << b << std::endl;
my_cast
};
// same type convert no problem
template<class T> void convert(const T src,T &dst)
{
dst=src;
std::cout << "Assign" << std::endl;
};
int main(void)
{
int i,j;
unsigned int n;
const int k=34;
double d;
std::string s;
convert(k,i); // const int , int (Convert T)
convert(i,j); // int , int (Convert T)
convert(j,d); // int , double (Convert S,D) --> cast
convert(j,n); // int , unsigned int (Convert S,D) --> cast
convert(j,s); // int , std::string (Convert S,D) --> stream convert
return 0;
}
--
-----------------------------------------------------------------------
Linux for ever
-----------------------------------------------------------------------
--
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 |
|
 |
Thomas Maeder Guest
|
Posted: Wed Sep 01, 2004 3:25 pm Post subject: Re: partial function template |
|
|
<bernd-news (AT) wiloyee (DOT) shnet.org> writes:
| Quote: | Hallo, ich versuche grade einige allgemeine Konvertierungsfunctionen
zu schreiben. Dazu versuche ich function templates zu benutzen. Leider
gibt es einige problme bei der function template Spezialisierung.
Muss ich klassen templates nehmen oder mach ich etwas anders falsch?
------------- Source code ----------
#include <iostream
#include
#include
template
{
D d;
dst=d;
std::cerr << "not implemented yet " << std::endl;
|
NB: Damit diese Ausgabeoperation zuverlässig funktioniert, musst Du auch
| Quote: | }
// ERROR uncommnet this function to compile
template<true,class S, class D> void my_cast(const S src, D &dst)
{
dst=static_cast<D>(src); //boost::num_cast dynamic_cast etc.
}
|
Du versucht, das Template von oben partiell zu spezialisieren. Partielle
Spezialisierung von Funktionstemplates gibt's aber in C++ nicht.
Der übliche Trick ist tatsächlich, die Funktionalität in ein Klassentemplate
zu verlagern, etwa:
template <bool cond, class S, class D>
void my_cast(S const &src, D &dst)
{
my_castHelper<cond,S,D>::execute(src,dst);
}
execute() ist hier ein statisches Member des Template my_castHelper.
Spezialisierungen von my_castHelper können eigene Implementationen von
execute() bereitstellen.
| Quote: | template<class S, class D> void convert(const S src,D &dst)
{
D d;
dst=d;
bool b= boost::is_convertible<S,D>::value;
std::cout << "Convert " << b << std::endl;
my_cast
};
// same type convert no problem
template<class T> void convert(const T src,T &dst)
{
dst=src;
std::cout << "Assign" << std::endl;
};
|
[Was sollen diese Strichpunkte nach den beiden Funktionsdefinitionen?]
Hier versuchst Du nicht zu spezialisieren, sondern zu überladen. Das ist
im Gegensatz zur partiellen Spezialisierung erlaubt.
--
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: Wed Sep 01, 2004 9:25 pm Post subject: Re: partial function template |
|
|
[email]bernd-news (AT) wiloyee (DOT) shnet.org[/email] wrote:
Realname waere nett/ueblich.
| Quote: | Hallo, ich versuche grade einige allgemeine Konvertierungsfunctionen
zu schreiben. Dazu versuche ich function templates zu benutzen. Leider
gibt es einige problme bei der function template Spezialisierung.
Muss ich klassen templates nehmen oder mach ich etwas anders falsch?
|
Partielle Template Spezialisierung gibt's fuer Funktionen deswegen
nicht, weil es (beinahe) dasselbe ist wie Ueberladung (mit
dem Unterschied, dass man etwas schwieriger den exakten Typ angeben
kann, um implizite Konvertierung zu verhindern, und manches mehr).
Um doch die Vorzuege von partieller Template Spezialisierung zu nutzen,
kann man Lokis type2type Template benutzen, das in "Modern C++ Design"
von Andrei Alexandrescu vorgestellt wird.
Aber funktioniert Ueberladung + Abfall-Template [1] in deinem Fall
nicht?
MfG,
Daniel Albuschat
[1] Das Template faengt dann alle Typen ab, fuer die es keine
Ueberladung gibt.
--
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 |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|