views:

487

answers:

3

Hey. Is it possible to overload operator<< for primitive types? Fx lets say that I want to write a std::endl each time want to write a int. Can I overload operator<< for int, so that it automatic puts a std::endl to the output? I have tried with this,

std::ostream& operator<<(std::ostream& strm, int & i)
{
   strm << i << std::endl;
   return strm;
}

but it doesn't work. I cant recall the compiler error message, but I think that I'm getting operator overloading all wrong any ways. I try to call the above overloaded operator<< in this way,

int main()
{
   int i = 2;
   std::out<<"Here is an int " << i;

   return 0;
}

But it doesn't work at all. Maybe I can't overload POD types?

+2  A: 

Remember that here you use << operator not only on int but also on ostream. You could derive from ostream and implement it in your own derived class, but I would suggest to make a simple macro like

#define EL(i) (i)<<std::endl

Alternatively you could make boxed int class and override the << for standard ostream and boxed int (like in answer by Iraimbilanja) class. Sounds like huge overkill but could work.

Muxecoid
+1  A: 

Your problem is that there is already an overload for operator << (ostream &, int), the one supplied by the C++ standard library. If you remove the overload definition and use:

#include <iostream>
int main()
{
   int i = 2;
   std::out<<"Here is an int " << i;

   return 0;
}

things work as expected.

And BTW, compiler error messages are kind of important, so it's a good idea to remember them and quote them in posts when asking questions.

edit - std::out above should of couse be std::cout

anon
I'm sorry that I can't provide an output of the error that the compiler prints out.
mslot
This doesn't solve the "append-an-endl" problem, does it?
xtofl
No it doesn't. It has nothing to do with me having a problem for a project or so. I was just wondering on how to overload, and if I could, overload a primitive type.
mslot
Yes, you can overload a primitive type, but you can only do so once. The compiler error is probably something like "operator<<(std::ostream) already defined."
Max Lybbert
+2  A: 

As zabzonk said, the standard library provides an (ostream&, int) overload so you can't define another.

To simulate what you were doing (though it is completely pointless in its present form :) :

class EndlinedInteger {
public:
    EndlinedInteger(int i) : i(i) { }
    friend ostream& operator<<(ostream&, EndlinedInteger const&);
private:
    int i;
};

ostream& operator<<(ostream& out, EndlinedInteger const& ei) {
    out << ei.i << endl;
    return out;
}

int main()
{
   EndlinedInteger i = 2;
   std::cout<<"Here is an int " << i;
}
Yes. It is completely pointless. But I didn't know if I could do it in my way, or if I had to do it in your way (which is even more pointless). But I'm just trying different things out.
mslot
And you need to provide all int operators and not only the constructor in your boxed int.
Muxecoid
sure i was just demonstrating. anyway, it depends on what he wants to do with his boxed int :)