Template stream operator overloading
I want to be able to put the template class below in a cin and cout and thereby be able to input/output the value contained within. Has anyone done this before and know how it could be done?
Code:
template<class T> SomeClass
{
public:
SomeClass(T inval) { value = inval; }
protected:
T value;
}
Example of what I want to be able to do:
Code:
SomeClass someObject<int>(5);
cout << someObject << endl;
When run, I want this code to output '5'. Thanks
Re: Template stream operator overloading
Quote:
Originally Posted by
Etherous
Has anyone done this before and know how it could be done?
Quite trivial to implement:
Code:
template<class T> class SomeClass
{
public:
SomeClass(T inval) : value(inval) {} //This is real initialization.
friend std::ostream& operator<<(std::ostream& outputStream, const SomeClass& instance)
{
return outputStream << instance.value;
}
protected:
T value;
};
EDIT: Forgot about the second part of your question; it's the same idea with operator>>, but you'll be dealing with std::istream (in the case of std::cin).
Re: Template stream operator overloading
Well, not surprisingly, it works. I feel stupid because it's such a simple thing to do. I just haven't needed to do this for quite some time. Thanks.
Re: Template stream operator overloading
Is there any approach that works with any class including different variable ?
This is tedious because you need to write this every time for every class.
Re: Template stream operator overloading
Quote:
Originally Posted by
Peter_APIIT
Is there any approach that works with any class including different variable ?
This is tedious because you need to write this every time for every class.
The compiler can't read your mind (yet). :sick:
Re: Template stream operator overloading
I mean does generic programming able to handle such a tedious task for every class.
Re: Template stream operator overloading
I have a template class called basic_posint, in which I have declared a friend function
Code:
template<unsigned BASE>
class base_posint : public integer {
...
public:
friend std::ostream& operator <<(std::ostream&, const basic_posint);
}
but the compiler gives me the following error:
Code:
integer.hpp:85: error: ISO C++ forbids declaration of `basic_posint' with no type
The function is declared as public. What does it mean, doesn't it recognize basic_posint as a typename?
Edit: I also tried your example template class, and it compiled without any warnings. On the other hand, if I replaced the function body by a semi colon (hence defining the function somewhere else), I got the following warning:
Code:
integer.hpp:48: warning: friend declaration `std::ostream& operator<<(std::ostream&, const SomeClass<T>&)' declares a non-template function
integer.hpp:48: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
Is it supposed to be like this? Do you have to define the function within the class to prevent this warning? (I'm compiling with g++)
Re: Template stream operator overloading
Quote:
Originally Posted by TriKri
I have a template class called basic_posint
Your class template is named base_posint. You need to decide if you want to use base_posint or basic_posint.
Quote:
Originally Posted by TriKri
Do you have to define the function within the class to prevent this warning? (I'm compiling with g++)
No. Read this FAQ on Why do I get linker errors when I use template friends? for a solution.
Re: Template stream operator overloading
Quote:
Originally Posted by
laserlight
Doh! XD Sometimes I feel really stupid. I replaced basic_posint with base_posint, and the problem got solved. Strange, huh? :P
Concerning the warnings I got from the compiler, I had to do both of the things that were mentioned in your link in order to get it to compile. If I remove the <> template indication I get a warning and then undefined reference to `operator<<(std::ostream&, base_posint<10u> const&)' and it won't compile, and if I remove the pre-declaration I get an error saying my friend function does not match any template declaration. But now it works just fine. Thanks!