T
Tom
Hello everybody,
please consider the following code:
I don't understand what the reason is; is template specialization
for a function type too much for templates?
Please help me to understand this.
Best regards, Thomas
please consider the following code:
Code:
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
int i = 12345;
// make endltype the type of std::endl; taken from one of these
// standard header files:
typedef ostream& (* endltype)(ostream&);
// make operator,(...) behave the same as operator<<(...)
// (this is just a minimized example; I'm not going to do
// this operator,(...) thing in real life)
template <typename T> ostream& operator,(ostream& os, T test)
{ os << test; return os; };
// two template specializations:
template<> ostream& operator,<int>(ostream& os, int i)
{ os << i; } //compiles
template<> ostream& operator,<endltype>(ostream& os, endltype e)
{ os << e; } // also compiles (!)
int main()
{ // verify my typedef for endltype is correct:
cout << "aaa" << (endltype) endl << "bbb"; // works
// verify the template works for int (and produces the
// expected result):
cout << "blah", i, "blah\n";
// comment this out and it will compile:
cout << "aaa", endl, "bbb";
// this one produces an error at compilation time:
// Fehler: right-hand operand of comma kann die Adresse
// der überladenen Funktion nicht auflösen
// In english:
// error: right-hand operand of comma cannot find address
// of overloaded function
}
I don't understand what the reason is; is template specialization
for a function type too much for templates?
Please help me to understand this.
Best regards, Thomas