friend template function with multiple types

B

Bonzo

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<typename 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
 
R

Rob Williscroft

Bonzo wrote in
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<typename 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.
 
V

Victor Bazarov

Bonzo said:
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<typename 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)

{
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;
}
 
B

Bonzo

Victor Bazarov said:
You forgot the semicolon here...


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
 
B

Bonzo

John Harrison said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top