Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I write what the friend template function in template class?

0 views
Skip to first unread message

maigre_dragon

unread,
Dec 22, 2009, 7:17:14 PM12/22/09
to
I write a template class.
example:
template < typename T >
class MemoryMap{
public:
MemoryMap(const T):data_(T){}
friend:
std::ostream& operator<< (std::ostream& out,MemoryMap<T>& x){
size_t length=sizeof(x.data_);
Byte* p=reinterpret_cast<Byte*>(&x.data_);
for (size_t i=0;i<length;i++)
{
assert( i<length );
out<<*p++<<' ';
}
return out;
}
private:
T data_;
};
question:
I need special a special fucntion that " std::ostream& operator<<
(std::ostream out, MemoryMap<char*>& x) ".
Is special template class or special template function?

Vladimir Jovic

unread,
Dec 23, 2009, 3:21:15 AM12/23/09
to

wow have you tried to compile your example? Or you just took from some
dodgy web site?
Here is corrected class:

template < typename T >
class MemoryMap{
public:

MemoryMap(const T dp):data_(dp){}


friend std::ostream& operator<< (std::ostream& out,MemoryMap<T>& x)
{
size_t length=sizeof(x.data_);

unsigned char* p=reinterpret_cast< unsigned char* >(&x.data_);

Saeed Amrollahi

unread,
Dec 23, 2009, 7:27:27 AM12/23/09
to

Hi

I don't understand what you mean. You want a special function as a
friend to MemoryMap class?
As you wrote above code (of course it has a few syntax errors!), after
each template instantiation, you have an appropriately typed output
function. For example
for MemoryMap<char> you have an operator<< for MemoryMap<char>, for
MemoryMap<string> you
have an operator<< for MemoryMap<string> and ...
If you want an specializaed friend function, I believe you can add the
following function to
your class:
friend std::ostream& operator<< (std::ostream& out,
MemoryMap<char*>& x)
{
/* ... */
}

Regards,
-- Saeed Amrollahi

maigre_dragon

unread,
Dec 23, 2009, 8:36:26 PM12/23/09
to

> Hi
>
> I don't understand what you mean. You want a special function as a
> friend to MemoryMap class?
> As you wrote above code (of course it has a few syntax errors!), after
> each template instantiation, you have an appropriately typed output
> function. For example
> for MemoryMap<char> you have an operator<< for MemoryMap<char>, for
> MemoryMap<string> you
> have an operator<< for MemoryMap<string> and ...
> If you want an specializaed friend function, I believe you can add the
> following function to
> your class:
> friend std::ostream& operator<< (std::ostream& out,
> MemoryMap<char*>& x)
> {
> /* ... */
> }
>
> Regards,
> -- Saeed Amrollahi

Ok! Yes, this is I mean.
Do I wirte?
//MemoryMap.hpp


template< typename T>
class MemoryMap{
public:
MemoryMap(const T):data_(T){}

friend std::ostream& operator<< (std::ostream& out, MemoryMap<T>& x);
private:
T data_;
};
//MemoryMap.cpp


std::ostream& operator<< (std::ostream& out, MemoryMap<char*>& x){

//snip
}
std::ostream& operator<< (std::ostream& out,MemoryMap<std::string>& x){
//snip
}

Saeed Amrollahi

unread,
Dec 24, 2009, 8:28:29 AM12/24/09
to
> }- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

AFAIK, You can, please try it. BTW, you can define template function
specialization,
but by each friend declaration, each instantiated function is a friend
to instantiated
MemoryMap class template.

Regards,
-- Saeed Amrollahi

0 new messages