473,414 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Template Friend

Given this code:
#include <iostream>
#include <string>

class timestamp
{
public:
timestamp() {
std::cout << "--> default constructor" << std::endl;
}

template <class charT, class traits>
friend
std::basic_ostream<charT, traits>&
operator<< (std::basic_ostream<charT, traits>& strm, const timestamp& ts);

private:
// implementation not relevant
};

template<class charT, class traits>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& strm, 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?

Changing to

friend
std::ostream&
operator<< (std::ostream& strm, const timestamp& ts);

and the compiler stops complaining.

Josue Gomes
josuegomes at yahoo dot com
Jul 22 '05 #1
1 2207
pa**************@yahoo.com wrote in news:49a09e7e.0406290413.3a5c018
@posting.google.com in comp.lang.c++:
template<class charT, class traits>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& strm,
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.

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 <iostream>
#include <ostream>

class timestamp
{
template < typename charT, typename traits>
friend std::basic_ostream<charT, traits>& operator<< (
std::basic_ostream<charT, traits>& strm, timestamp const& ts
)
{
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/
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christophe Barbe | last post by:
I am not clear about friend functions of a template class. GCC (3.3.2) wants me to add <> after the friend function names in the class declaration and VisualC++ doesn't like that. template...
2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
1
by: Dmitry D | last post by:
Hi all, I'm having problems with declaring a template friend function. It seems like I've done everything as explained in C++ FAQ, but still, I'm getting the linker error (unresolved external...
5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
9
by: Adam Badura | last post by:
I have code like this template<int size> big_int { /* ... */ template<int size2> friend class big_int<size2>; }; What I wanted to achive is to be able to easly convert different sized...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
9
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.