template specialization with function types question

T

Tom

Hello everybody,
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top