g++ 4.8.1 and clang++ 3.4 give different results for next code:
// simplified code from a Logger module
#include <iostream>
template<class T> void tf(const T*) { // clang++
std::cout << "void tf(const T*)\n";
}
template<class T> void tf(T) { // g++
std::cout << "void tf(T)\n";
}
int main(){
typedef std::ios_base& (*ph)(std::ios_base&);
ph p = std::hex;
tf(p); // or just tf(std::hex)
}
I can't figure out which variant is correct (C++ 03).
You may overload a function template either by a non-template function or by another function template. The function call f(1, 2) could match the argument types of both the template function and the non-template function.
Template Function Overloading:The name of the function templates are the same but called with different arguments is known as function template overloading. If the function template is with the ordinary template, the name of the function remains the same but the number of parameters differs.
A template is a simple yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that we don't need to write the same code for different data types. For example, a software company may need to sort() for different data types.
A pointer to function is not a pointer to an object and talking about const
-ness of a pointer to function doesn't make sense in C++.
IMO g++ is right because hex
qualifies as a pointer to function, but not as a const *
to anything.
In the first case the template parameter is not a "pointer" but a "pointer to object".
There's no such a thing as a generic "pointer" in C++... you have pointers to function, pointers to object or pointer to members. Each of the three has different rules and is incompatible with the others.
Admittedly the null pointer brings in some confusion...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With