D
desktop
If a function should work with different types you normally overload it:
void myfun(int a){
// do int stuff
}
void myfun(std::string str){
// do string stuff
}
void myfun(double d){
// do double stuff
}
But you can also use a specialized template function instead:
template<typename T>
void mytemp(T) {
// "default case" if no other match
}
template<>
void mytemp(int a) {
// in case of int
}
template<>
void mytemp(std::string str) {
// in case of string
}
template<>
void mytemp(double d) {
// in case of double
}
The only difference I have found is the correct template is only used if
the arguments match exactly:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.11
But is that the only difference? Are the types decided at compile time
in both examples etc?
void myfun(int a){
// do int stuff
}
void myfun(std::string str){
// do string stuff
}
void myfun(double d){
// do double stuff
}
But you can also use a specialized template function instead:
template<typename T>
void mytemp(T) {
// "default case" if no other match
}
template<>
void mytemp(int a) {
// in case of int
}
template<>
void mytemp(std::string str) {
// in case of string
}
template<>
void mytemp(double d) {
// in case of double
}
The only difference I have found is the correct template is only used if
the arguments match exactly:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.11
But is that the only difference? Are the types decided at compile time
in both examples etc?