template specialization with function types question

Discussion in 'C++' started by Tom, Mar 31, 2010.

  1. Tom

    Tom Guest

    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
     
    Tom, Mar 31, 2010
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    4
    Views:
    7,716
    pdixtl
    Jun 4, 2010
  2. Ruben Campos
    Replies:
    3
    Views:
    6,496
  3. Joseph Turian
    Replies:
    4
    Views:
    610
    John Carson
    Mar 20, 2006
  4. Joseph Turian
    Replies:
    2
    Views:
    481
  5. Replies:
    2
    Views:
    353
Loading...

Share This Page