Re: template specialization with function types question
Tom wrote:
#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; };
You can write it in one statement BTW:
return os << test;
// two template specializations:
template<> ostream& operator,<int>(ostream& os, int i)
{ os << i; } //compiles
Where is the 'return'?
template<> ostream& operator,<endltype>(ostream& os, endltype e)
{ os << e; } // also compiles (!)
Where is the 'return'?
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";
Really? <shrug> Your function (operator,) may compile, but it has
undefined behavior if there is no 'return'.
// comment this out and it will compile:
cout << "aaa", endl, "bbb";
It might. And it might not. Undefined behavior anyway.
// 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
}
[/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.
Fix the code first. Then try
cout << "blah", std::endl, "bbb";
It's possible that your compiler can't locate std::endl without ADL.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask