473,405 Members | 2,282 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,405 software developers and data experts.

Templated friends

I'm trying to make a templated function a friend like this:

class cls
{
...
friend cls func<> (const cls& a, const cls& b);
};

template< template<class> class Op>
cls func (const cls& a, const cls& b)
{
...
}

That seems to be what TC++PL SE shows in C.13.2, but the compiler seems to
be interpreting it as a data declaration:
error C2143: syntax error : missing ';' before '<'
error C2460: 'func' : uses 'cls', which is being defined: see declaration of
'cls'
error C2433: 'func' : 'friend' not permitted on data declarations
error C2238: unexpected token(s) preceding ';'
The compiler docs on those diagnostics don't seem to have anything to do
with friend.

What am I doing wrong?

Thanks!
- Pete
Jul 22 '05 #1
5 5621
"Pete C." <x@x.x> wrote...
I'm trying to make a templated function a friend like this:

class cls
{
...
friend cls func<> (const cls& a, const cls& b);
};

template< template<class> class Op>
cls func (const cls& a, const cls& b)
{
The definition of this function has no 'Op' used. There is no
way for a compiler to figure out the template argument from the
function arguments. That's why you have to always explicitly
specify it.
...
}

That seems to be what TC++PL SE shows in C.13.2,
I don't have my copy of the SE here (it's at work), are you sure
the example is precisely as you show here?
but the compiler seems to
be interpreting it as a data declaration:
error C2143: syntax error : missing ';' before '<'
error C2460: 'func' : uses 'cls', which is being defined: see declaration of 'cls'
error C2433: 'func' : 'friend' not permitted on data declarations
error C2238: unexpected token(s) preceding ';'
The compiler docs on those diagnostics don't seem to have anything to do
with friend.

What am I doing wrong?


Perhaps you're not following the rules of syntax. Why do you
think what you wrote should work? Where did you see the syntax
you're using?

'func' is a function _template_. If you want a _template_ to be
a friend (which actually means that _all_possible_instantiations_
of that template are friends), you need to begin your declaration
with the keyword "template". If you want one _particular_
instance of that template to be a friend, you need to give the
template _argument_ (put something in the <>).

Of course it's entirely possible that you're not using the right
compiler for the code. Older VC++ can't handle template friends
very well.

Victor
Jul 22 '05 #2
Ian
Pete C. wrote:
I'm trying to make a templated function a friend like this:

class cls
{
...
friend cls func<> (const cls& a, const cls& b);
};

template< template<class> class Op>
cls func (const cls& a, const cls& b)
{
...
}

That seems to be what TC++PL SE shows in C.13.2, but the compiler seems to
be interpreting it as a data declaration:
error C2143: syntax error : missing ';' before '<'
error C2460: 'func' : uses 'cls', which is being defined: see declaration of
'cls'
error C2433: 'func' : 'friend' not permitted on data declarations
error C2238: unexpected token(s) preceding ';'
The compiler docs on those diagnostics don't seem to have anything to do
with friend.

What am I doing wrong?

Each instance of a template is unique. So (unfortunately) you have to
be explicit;

class cls;

template< class Op>
cls func(const cls& a, const cls& b)
{
....
}

class cls
{
friend cls func<int> (const cls& a, const cls& b);
};

Ian
Jul 22 '05 #3
Victor Bazarov wrote:
"Pete C." <x@x.x> wrote...
I'm trying to make a templated function a friend like this:

class cls
{
...
friend cls func<> (const cls& a, const cls& b);
};

template< template<class> class Op>
cls func (const cls& a, const cls& b)
{
The definition of this function has no 'Op' used. There is no
way for a compiler to figure out the template argument from the
function arguments. That's why you have to always explicitly
specify it.
...
}

That seems to be what TC++PL SE shows in C.13.2,


I don't have my copy of the SE here (it's at work), are you sure
the example is precisely as you show here?


template<class T> class Matrix;

template<class T> class Vector {
T v[4];
public:
friend Vector operator*<>(const Matrix<T>&, const Vector&);
// ...
};

template<class T> class Matrix {
Vector<T> v[4];
public:
friend Vector<T> operator*<>(const Matrix&, const Vector<T>&);
// ...
};

template<class T> Vector<T> operator*(const Matrix<T>& m, const Vector<T>&
v)
{
// ...
}
but the compiler seems to
be interpreting it as a data declaration:
error C2143: syntax error : missing ';' before '<'
error C2460: 'func' : uses 'cls', which is being defined: see
declaration of 'cls'
error C2433: 'func' : 'friend' not permitted on data declarations
error C2238: unexpected token(s) preceding ';'
The compiler docs on those diagnostics don't seem to have anything
to do with friend.

What am I doing wrong?
Perhaps you're not following the rules of syntax. Why do you
think what you wrote should work? Where did you see the syntax
you're using?

'func' is a function _template_. If you want a _template_ to be
a friend (which actually means that _all_possible_instantiations_
of that template are friends), you need to begin your declaration
with the keyword "template". If you want one _particular_
instance of that template to be a friend, you need to give the
template _argument_ (put something in the <>).


I changed it to:
template< template<class> class Op> friend variant binaryoperator (const
variant& a, const variant& b);
and it worked!

I had tried it with 'friend' before 'template<...>', but that failed. Then I
looked at TC++PL and its complete lack of 'template<...>' made me think that
it was unneccessary.

Thanks again!

Of course it's entirely possible that you're not using the right
compiler for the code. Older VC++ can't handle template friends
very well.
It's VC++ 7.1, so I can pretty much blame any errors on myself ;)

- Pete

Victor

Jul 22 '05 #4
Ian wrote:
Pete C. wrote:
I'm trying to make a templated function a friend like this:

class cls
{
...
friend cls func<> (const cls& a, const cls& b);
};

template< template<class> class Op>
cls func (const cls& a, const cls& b)
{
...
}

That seems to be what TC++PL SE shows in C.13.2, but the compiler
seems to
be interpreting it as a data declaration:
error C2143: syntax error : missing ';' before '<'
error C2460: 'func' : uses 'cls', which is being defined: see
declaration of
'cls'
error C2433: 'func' : 'friend' not permitted on data declarations
error C2238: unexpected token(s) preceding ';'
The compiler docs on those diagnostics don't seem to have anything to do
with friend.

What am I doing wrong?

Each instance of a template is unique. So (unfortunately) you have to
be explicit;


You can make a template friend. friends are specified as declarations,
so if you can declare it, you can make a friend out of it (in theory).

class cls;

template< class Op>
cls func(const cls& a, const cls& b);

class cls
{
template< class Op>
friend cls func(const cls& a, const cls& b);
};
.... now, that's not a very interesting template, maybe the OP wanted
somthing like this:

class cls;

template<class TA, class TB>
cls func(const TA& a, const TB& b);

class cls
{
template<class TA, class TB>
friend cls func(const TA& a, const TB& b);
};
Jul 22 '05 #5
Gianni Mariani wrote:
<snip>

You can make a template friend. friends are specified as
declarations, so if you can declare it, you can make a friend out of
it (in theory).

class cls;

template< class Op>
cls func(const cls& a, const cls& b);

class cls
{
template< class Op>
friend cls func(const cls& a, const cls& b);
};

I had tried:
friend template<...> cls func ... ;

Then TC++PL confused me with the syntax as shown in the topic post, but with
a nudge from Victor Bazarov I came the correct syntax. :)

... now, that's not a very interesting template, maybe the OP wanted
somthing like this:

class cls;

template<class TA, class TB>
cls func(const TA& a, const TB& b);

class cls
{
template<class TA, class TB>
friend cls func(const TA& a, const TB& b);
};


I actually had: template<template<class> class Op>
To pass a template sort of like this:
cls = func<std::plus>(cls(123), cls(321))

- Pete
Jul 22 '05 #6

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

Similar topics

3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
1
by: Rich | last post by:
Hi, I have a query regarding VC6 and its handling of templated copy constructors. Here goes: Take a look at the following code sample... template<class _Ty, size_t t_uiSize = 10 > class...
2
by: ferdinand.stefanus | last post by:
Hi, I have some questions regarding templated class constructor: #include <iostream> using namespace std; template<typename T> class Foo { public:
4
by: Lionel B | last post by:
Greetings, The following code: <code> template<typename T> class A { protected:
6
by: Alex | last post by:
I have been loving the templated datacolumns of the datagrid. I have stumbled onto a problem though that is beyond by knowledge and I was hoping someone here could jumpstart me. My templated...
2
by: mattjgalloway | last post by:
I'm having some problems with a templated member function of a templated class. Unfortunately I can't replicate it with a simple example so I know something odd must be going on!!! Basically it's...
7
by: Claudius | last post by:
Hello, in my class TopTen I need to define three constructors while only the last one, the most general in terms of templates, should be sufficient in my opinion: template <typename Tnum,...
2
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
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.