C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template-Paremeter ohne "deduction"

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German)
View previous topic :: View next topic  
Author Message
Rolf Magnus
Guest





PostPosted: Tue Apr 05, 2005 9:02 pm    Post subject: Template-Paremeter ohne "deduction" Reply with quote



Gibt es eine Möglichkeit, bei einem Funktionstemplate

template <typename T>
void funktion(T value);

zu verhindern, daß aus dem Typ des Funktionsparameters automatisch der
Template-Parameter abgeleitet wird?
Der Grund für diesen Wunsch ist, daß es öfters vorkommen kann, daß ich einen
Wert eines anderen als des gewünschten Typs als Parameter übergebe. Der muß
dann unbedingt in den richtigen Typ konvertiert werden, also z.B. sowas
wie:

short x;
//...
funktion<int>(x);

oder

funktion(static_cast<int>(x));

Damit man das nicht versehentlich vergisst, hätte ich es gerne, daß man den
Template-Paremeter auf jeden Fall explizit angeben muß. Also z.B. bei:

funktion(x); //ups, short statt int

eine Fehlermeldung kommt, weil ich den Zieltyp nicht angegeben habe. Gibt es
irgendeinen Trick, der sowas ermöglicht?

--
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 Moll
Guest





PostPosted: Tue Apr 05, 2005 9:47 pm    Post subject: Re: Template-Paremeter ohne "deduction" Reply with quote



Hallo

Rolf Magnus wrote:

Quote:
Damit man das nicht versehentlich vergisst, hätte ich es gerne, daß man
den Template-Paremeter auf jeden Fall explizit angeben muß. Also z.B. bei:

funktion(x); //ups, short statt int

eine Fehlermeldung kommt, weil ich den Zieltyp nicht angegeben habe. Gibt
es irgendeinen Trick, der sowas ermöglicht?

Ich glaube schon:

template<typename T>
struct type_identity
{
typedef T type;
};

template<typename T>
T funktion(const typename type_identity<T>::type& x)
{
return x;
}

Hier kann der Typ T nicht mehr vom Compiler hergeleitet werden. Daher muß er
explizit angegeben werden:

int main() { funktion(3); }

(g++)
In function `int main()':
error: no matching function for call to `funktion(int)'

Stattdessen ist es nötig, z.B. zu schreiben:
int main() { funktion<char>(3); }

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
Thomas Maeder
Guest





PostPosted: Tue Apr 05, 2005 9:59 pm    Post subject: Re: Template-Paremeter ohne "deduction" Reply with quote



Rolf Magnus <ramagnus (AT) t-online (DOT) de> writes:

Quote:
Gibt es eine Möglichkeit, bei einem Funktionstemplate

template <typename T
void funktion(T value);

zu verhindern, daß aus dem Typ des Funktionsparameters automatisch der
Template-Parameter abgeleitet wird?

template void funktion(U value);

--
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
Falk Tannhäuser
Guest





PostPosted: Tue Apr 05, 2005 10:13 pm    Post subject: Re: Template-Paremeter ohne "deduction" Reply with quote

Rolf Magnus wrote:

Quote:
Gibt es eine Möglichkeit, bei einem Funktionstemplate

template <typename T
void funktion(T value);

zu verhindern, daß aus dem Typ des Funktionsparameters automatisch der
Template-Parameter abgeleitet wird?

Man kann ausnutzen, dass es für Klassentemplates im Gegensatz zu
Funktionstemplates keine automatische Typdeduktion gibt:

template {
call_funktion(T const& t) { funktion(t); }
};
....

call_funktion(42); // KO, geht nich!
call_funktion<int>(42); // OK

(Öfter macht man's ja gerade umgekehrt: Man schreibt ein Funktions-
template, um ein Klassentemplate zu instanzieren, wenn man für
letzteres die automatische Typdeduktion haben will - siehe z.B.
std::make_pair().)

MfG
Falk

--
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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.