 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
paidojoao-groups@yahoo.co Guest
|
Posted: Tue Jun 29, 2004 12:13 pm Post subject: Template Friend |
|
|
Given this code:
#include <iostream>
#include <string>
class timestamp
{
public:
timestamp() {
std::cout << "--> default constructor" << std::endl;
}
template
friend
std::basic_ostream<charT, traits>&
operator<< (std::basic_ostream
private:
// implementation not relevant
};
template<class charT, class traits>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream
{
using namespace std;
strm << "--> operator<<()";
return strm;
}
int main()
{
timestamp p;
std::cout << p << std::endl; // line 36
return 0;
}
-------------------------------------------
VC 7.1 (13.10.3077) complains with
bug.cpp(36) : error C2563: mismatch in formal parameter list
bug.cpp(36) : error C2568: '<<' : unable to resolve function overload
Borland C++ 5.6.4 and G++ 3.2 compile without errors.
Who is wrong and who is right?
Changing to
friend
std::ostream&
operator<< (std::ostream& strm, const timestamp& ts);
and the compiler stops complaining.
Josue Gomes
josuegomes at yahoo dot com
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Tue Jun 29, 2004 12:58 pm Post subject: Re: Template Friend |
|
|
[email]paidojoao-groups (AT) yahoo (DOT) com[/email] wrote in news:49a09e7e.0406290413.3a5c018
@posting.google.com in comp.lang.c++:
| Quote: | template<class charT, class traits
std::basic_ostream
operator<<(std::basic_ostream
const timestamp& ts)
{
using namespace std;
strm << "--> operator<<()";
return strm;
}
int main()
{
timestamp p;
std::cout << p << std::endl; // line 36
return 0;
}
-------------------------------------------
VC 7.1 (13.10.3077) complains with
bug.cpp(36) : error C2563: mismatch in formal parameter list
bug.cpp(36) : error C2568: '<<' : unable to resolve function overload
Borland C++ 5.6.4 and G++ 3.2 compile without errors.
Who is wrong and who is right?
|
VC7.1 is wrong you've found a bug.
| Quote: |
Changing to
friend
std::ostream&
operator<< (std::ostream& strm, const timestamp& ts);
and the compiler stops complaining.
|
Yes, but this isn't the same function as your template function
above, simply removing the friend declaration also works-around
the bug.
Workaround: define the friend inline in the class, if its implementation
is unsutible for inlining have it forward to a non-inline template member
instead.
#include
#include <ostream>
class timestamp
{
template < typename charT, typename traits>
friend std::basic_ostream<charT, traits>& operator<< (
std::basic_ostream
)
{
return ts.m_print( strm );
}
private:
template < typename charT, typename traits>
std::basic_ostream<charT, traits>&
m_print( std::basic_ostream<charT, traits>& ) const;
};
template < typename charT, typename traits>
std::basic_ostream<charT, traits>&
timestamp::m_print( std::basic_ostream<charT, traits>& strm) const
{
strm << "--> operator<<()";
return strm;
}
int main()
{
timestamp p;
std::cout << p << std::endl;
}
Unfortunatly this doesn't work for Borland C++ 5.6.4, so your
left with making m_print() public and just delaring/defining
operator << () at namespace scope and forwarding to m_print.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|