473,799 Members | 2,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

friend template function with multiple types

I'm trying to write a class for a smart pointer. I'm having a problem
though when trying to implement operator== with both sides being a Smart.
(Or any function that I try to bring U into.)
The pointer contained might not be the exact same type but still get
tested for equality. Here's what I have:
template <typename T> class Smart;

template<typena me T, typename U>
bool operator== ( const Smart<T>& a, const Smart<U>& b );

template <typename T> class Smart
{
/* On the next line, GCC says:
"type/value mismatch at argument 1 in template parameter list for
`template<class T> class Smart expected a type, got `U'"
*/

friend bool operator== ( const Smart<T>& a, const Smart<U>& b );
// friend bool operator==<> ( const Smart<T>& a, const Smart<U>& b );

}

bool operator== (const Smart<T>& a, const<SmartU>& b)
{
return true;
}
It also warns about the angle brackets not being there, which are in the
commented out line, but adding them does not solve the error.

What must I do to make the compiler happy?

Thanks,
-Jim
Jul 19 '05 #1
4 5559
Bonzo wrote in
news:do******** *************** *@syrcnyrdrs-02-ge0.nyroc.rr.co m:
I'm trying to write a class for a smart pointer. I'm having a problem
though when trying to implement operator== with both sides being a
Smart. (Or any function that I try to bring U into.)
The pointer contained might not be the exact same type but still get
tested for equality. Here's what I have:
template <typename T> class Smart;

template<typena me T, typename U>
bool operator== ( const Smart<T>& a, const Smart<U>& b );

template <typename T> class Smart
{
/* On the next line, GCC says:
"type/value mismatch at argument 1 in template parameter list
for
`template<class T> class Smart expected a type, got `U'"
*/
// Overkill but harmless:
template <typename A, typename B>
friend bool operator == (Smart<A> const &a, Smart<B> const &b);
// But why not just make it a member function?

template <typename U>
bool operator == (Smart<U> const &rhs) const
{
return false /* or whatever */;
}

friend bool operator== ( const Smart<T>& a, const Smart<U>& b );
// friend bool operator==<> ( const Smart<T>& a, const Smart<U>& b );

}

bool operator== (const Smart<T>& a, const<SmartU>& b)
{
return true;
}
It also warns about the angle brackets not being there, which are in
the commented out line, but adding them does not solve the error.

What must I do to make the compiler happy?


HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #2
"Bonzo" <do**@email.m e> wrote...
I'm trying to write a class for a smart pointer. I'm having a problem
though when trying to implement operator== with both sides being a Smart.
(Or any function that I try to bring U into.)
The pointer contained might not be the exact same type but still get
tested for equality. Here's what I have:
template <typename T> class Smart;

template<typena me T, typename U>
bool operator== ( const Smart<T>& a, const Smart<U>& b );

template <typename T> class Smart
{
/* On the next line, GCC says:
"type/value mismatch at argument 1 in template parameter list for
`template<class T> class Smart expected a type, got `U'"
*/

friend bool operator== ( const Smart<T>& a, const Smart<U>& b );
You have to say that this is from a template:

template<class T, class U> friend bool operator ==(...

otherwise, what's "U"?

And unfortunately, it's impossible to make a partial specialisation
a friend.
// friend bool operator==<> ( const Smart<T>& a, const Smart<U>& b );

} ^^^
You forgot the semicolon here...

bool operator== (const Smart<T>& a, const<SmartU>& b)
Again, this is not right. What's "T" and "U"? What's const<SmartU>?
Why don't you post the real code instead of typing it here?
{
return true;
}
It also warns about the angle brackets not being there, which are in the
commented out line, but adding them does not solve the error.

What must I do to make the compiler happy?


You probably want to find a good book on templates.

Here is what compiles:
----------------------------------------
template<class T> class Smart;

template<class T,class U>
bool operator== ( const Smart<T>&, const Smart<U>&);

template<class T> class Smart
{
template<class S, class U>
friend bool operator==( const Smart<S>&, const Smart<U>&);
};

template<class T, class U>
bool operator== (const Smart<T>& a, const Smart<U>& b)
{
return true;
}

int main()
{
Smart<int> si;
Smart<bool> sb;
si == sb;
}

----------------------------------------

HTH

Victor
Jul 19 '05 #3
In article <vg************ @corp.supernews .com>,
"Victor Bazarov" <v.********@att Abi.com> wrote:
You forgot the semicolon here...

bool operator== (const Smart<T>& a, const<SmartU>& b)


Again, this is not right. What's "T" and "U"? What's const<SmartU>?
Why don't you post the real code instead of typing it here?


Good question. I made a stripped down version just to demonstrate the
error. I actually *did* compile it, getting the same error, was
satisfied, and posted it. Unfortunately, there were plenty of new errors
after the one I posted about.

After spending the better part of the day upping my template knowledge,
stripping out all the friend stuff for member functions, and making
changes similar to your code, not only does it work but I understand it.
Many thanks.

-Jim
Jul 19 '05 #4
In article <be************ @ID-196037.news.uni-berlin.de>,
"John Harrison" <jo************ *@hotmail.com> wrote:
First of I'd question the need to compare pointers of different type, normal
pointers don't work like that so why should smart pointers?
Yeah, I ripped that code out. I wouldn't be using it. It was in some
smart pointer code I was basing mine off of.
Secondly I'd question the need for friendship at all. Most smart pointers
have a member function that lets you get at the raw pointer


As did I, but I didn't really understand what was going on enough. Now
that I do, I kicked my friends out and added some member functions
instead.

Thank you everyone for the replies.

-Jim
Jul 19 '05 #5

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

Similar topics

1
2820
by: Alexander Stippler | last post by:
Hi what's wrong about the following code? It does not compile. template <typename T> class A { public: class B; };
2
10163
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 online at http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.15 Below are the two files that I compile with g++ foo.cpp -o foo or
5
2722
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: //**************************************************** // testClass.h //**************************************************** #ifndef TESTCLASS_H
5
1455
by: James Aguilar | last post by:
Hello, all. I saw this in the provided code of a lab that I have due in a couple of weeks in my algorithms class. It compiled fine in g++ but did not compile with the VC++ compiler. It does not recognize the angle brackets that follow "<<" in the operator friend declaration. --- CODE --- template <class T> class PriorityQueue {
5
2642
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> operator- (const MyTemplate <T> & object1, const MyTemplate <T> & object2); template <typename T> class MyTemplate
2
2508
by: xuatla | last post by:
The following is just a sample code to demostrate my question: ----------- template <typename T> class C { public: friend void f1(double i=2) { std::cout << i; } ; };
9
4036
by: rtalbot | last post by:
I've got a container that looks like this: template <class T> class Foo { public: Foo() : _data(), _status(1) { } Foo(T) : _data(T), _status(0) { } ~Foo() { }
9
2066
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
21
4748
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code that gets the same result) works as expected in visualc++. I know that this is probably not the right behavior for a compiler but it's the kind of behavior I'm searching for so I was hoping there was a way to do the same thing in gcc. As you know...
0
9686
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9540
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10475
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10250
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10026
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.